๋ฌธ์ ์ค๋ช
๋จธ์ฑ์ด๋ ์น๊ตฌ์๊ฒ ๋ชจ์ค๋ถํธ๋ฅผ ์ด์ฉํ ํธ์ง๋ฅผ ๋ฐ์์ต๋๋ค. ๊ทธ๋ฅ์ ์ฝ์ ์ ์์ด ์ด๋ฅผ ํด๋
ํ๋ ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ ค๊ณ ํฉ๋๋ค. ๋ฌธ์์ด 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'
}
'.-':'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;
}
}
'๐ ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ๊ณต ๋์ง๊ธฐ (0) | 2022.10.28 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ์์ด๊ฐ ์ซ์ด์ (0) | 2022.10.28 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 1 : ์ต์ ์ง์ฌ๊ฐํ (0) | 2022.10.26 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ์ง๋ฃ์์ ์ ํ๊ธฐ (0) | 2022.10.26 |
[ํ๋ก๊ทธ๋๋จธ์ค][JAVA]Level 0 : ํ ๋ฒ๋ง ๋ฑ์ฅํ ๋ฌธ์ (0) | 2022.10.26 |