๋ฌธ์
๋จธ์ฑ์ด๋ ์น๊ตฌ์๊ฒ ๋ชจ์ค๋ถํธ๋ฅผ ์ด์ฉํ ํธ์ง๋ฅผ ๋ฐ์์ต๋๋ค. ๊ทธ๋ฅ์ ์ฝ์ ์ ์์ด ์ด๋ฅผ ํด๋
ํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ ค๊ณ ํฉ๋๋ค. ๋ฌธ์์ด 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 ํฉ๋๋ค.
** a ~ z์ ํด๋นํ๋ ๋ชจ์ค๋ถํธ๊ฐ ์์๋๋ก ๋ด๊ธด ๋ฐฐ์ด์ ๋๋ค.
{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}
ํ์ด
์ ์ถ ์ฝ๋
class Solution {
public String solution(String letter) {
String answer = "";
//๋งค๊ฐ๋ณ์ letter์ ๊ณต๋ฐฑ(" ")์ ๊ธฐ์ค์ผ๋ก ์๋ผ์ ๋ฐฐ์ด์ ์ ์ฅ
String[] mos = letter.split(" ");
for(int i=0; i<mos.length; i++){
switch(mos[i]){
case ".-":
answer += "a";
break;
case "-...":
answer += "b";
break;
case "-.-.":
answer += "c";
break;
case "-..":
answer += "d";
break;
case ".":
answer += "e";
break;
case "..-.":
answer += "f";
break;
case "--.":
answer += "g";
break;
case "....":
answer += "h";
break;
case "..":
answer += "i";
break;
case ".---":
answer += "j";
break;
case "-.-":
answer += "k";
break;
case ".-..":
answer += "l";
break;
case "--":
answer += "m";
break;
case "-.":
answer += "n";
break;
case "---":
answer += "o";
break;
case ".--.":
answer += "p";
break;
case "--.-":
answer += "q";
break;
case ".-.":
answer += "r";
break;
case "...":
answer += "s";
break;
case "-":
answer += "t";
break;
case "..-":
answer += "u";
break;
case "...-":
answer += "v";
break;
case ".--":
answer += "w";
break;
case "-..-":
answer += "x";
break;
case "-.--":
answer += "y";
break;
case "--..":
answer += "z";
break;
}
}
return answer;
}
}
switch๋ฌธ์ ์ฌ์ฉํด a~z๊น์ง ํด๋นํ๋ ๋ชจ์ค๋ถํธ์ ๋ง๋ case๋ฅผ ์ฃผ์ด answer ๋ฌธ์์ด์ ์ถ๊ฐ๋๋๋ก ํ์๋ค.
'๐ Algorithm > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ํฉํ ๋ฆฌ์ผ(์๋ฐ) (0) | 2023.03.29 |
---|---|
[Programmers] ํผ๋ณด๋์น ์(์๋ฐ) (0) | 2023.03.28 |
[Programmers] ๋ฐ๋ณต๋ฌธ ์ถ๋ ฅํ๊ธฐ(์๋ฐ) (0) | 2023.03.25 |
[Programmers] ํผ์ ๋๋ ๋จน๊ธฐ(2)(์๋ฐ) (0) | 2023.03.21 |
[Programmers] ์ต๋น๊ฐ ๊ตฌํ๊ธฐ(์๋ฐ) (0) | 2023.03.20 |