Deep_Dev
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

๋จธ์“ฑ์ด๋Š” ์นœ๊ตฌ์—๊ฒŒ ๋ชจ์Šค๋ถ€ํ˜ธ๋ฅผ ์ด์šฉํ•œ ํŽธ์ง€๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ์€ ์ฝ์„ ์ˆ˜ ์—†์–ด ์ด๋ฅผ ํ•ด๋…ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด letter๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, letter๋ฅผ ์˜์–ด ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.
๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

morse = { 
    '.-':'a','-...':'b','-.-.':'c','-..':'d','.':'e','..-.':'f',
    '--.':'g','....':'h','..':'i','.---':'j','-.-':'k','.-..':'l',
    '--':'m','-.':'n','---':'o','.--.':'p','--.-':'q','.-.':'r',
    '...':'s','-':'t','..-':'u','...-':'v','.--':'w','-..-':'x',
    '-.--':'y','--..':'z'
}
์ œํ•œ์‚ฌํ•ญ
  • 1 ≤ letter์˜ ๊ธธ์ด ≤ 1,000
  • return๊ฐ’์€ ์†Œ๋ฌธ์ž์ž…๋‹ˆ๋‹ค.
  • letter์˜ ๋ชจ์Šค๋ถ€ํ˜ธ๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • letter์— ๊ณต๋ฐฑ์€ ์—ฐ์†์œผ๋กœ ๋‘ ๊ฐœ ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ•ด๋…ํ•  ์ˆ˜ ์—†๋Š” ํŽธ์ง€๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํŽธ์ง€์˜ ์‹œ์ž‘๊ณผ ๋์—๋Š” ๊ณต๋ฐฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ (letter / result )
".... . .-.. .-.. ---" "hello"
".--. -.-- - .... --- -." "python"
์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • .... = h
  • . = e
  • .-.. = l
  • .-.. = l
  • --- = o
  • ๋”ฐ๋ผ์„œ "hello"๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • .--. = p
  • -.-- = y
  • - = t
  • .... = h
  • --- = o
  • -. = n
  • ๋”ฐ๋ผ์„œ "python"์„ return ํ•ฉ๋‹ˆ๋‹ค.

 


 

letter๋งŒ split์ด์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋งŒ๋“ค์–ด์ฃผ๊ณ 

for๋ฌธ๋Œ๋ฉด์„œ ํ•ด๋‹น ๋ชจ์Šค๋ถ€ํ˜ธ๋ฅผ ์ฐพ์•„ ํ•ด๋‹นํ•˜๋Š” index๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์•ŒํŒŒ๋ฒณ ๋ฐฐ์—ด์—์„œ ์ถ”๊ฐ€ํ•˜๋ฉด ๋

 

class Solution {
    public String solution(String letter) {
        String answer = "";
        // a~z ๋ชจ์Šค๋ถ€ํ˜ธ
        String[] mos={".-","-...","-.-.","-..",".","..-.","--.","....","..",
                ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
                "...","-","..-","...-",".--","-..-","-.--","--.."};
        String[] alpha={"a","b","c","d","e","f","g","h","i","j","k","l","m",
                "n","o","p","q","r","s","t","u","v","w","x","y","z"};
        String[] s=letter.split(" "); // ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๋ฐฐ์—ด์— ๋‹ด์Œ
        for(int i=0; i<s.length; i++){ // letter๋ฌธ์ž ํ•˜๋‚˜์”ฉ
          for(int j=0; j<mos.length; j++){ // mos๋ถ€ํ˜ธ์—์„œ ๊ฒ€์ƒ‰
              if(s[i].equals(mos[j])) // ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด
                  answer+=alpha[j]; // ํ•ด๋‹น ๋ชจ์Šค๋ถ€ํ˜ธ์˜ index๊ฐ’์„ alpha์—์„œ ์ฐพ์•„์„œ ์ถ”๊ฐ€
          }
        }
        return answer;
    }
}