๐ 1. ๋ฌธ์ ์ค๋ช

์ ์ถ๋ ฅ ์
letter | result |
".... . .-.. .-.. ---" | "hello" |
".--. -.-- - .... --- -." | "python" |
๐ก 2. ์ ๊ทผ๋ฐฉ์
์ฃผ์ด์ง ๋ชจ์ค๋ถํธ๋ฅผ morse.put() ์ผ๋ก ํ ์ค์ฉ ํด์๋งต์ ์ถ๊ฐ ํด์ผ ํ๋ค๋ ๋ถ๋ถ์ด ๊ท์ฐฎ์ ๋ฌธ์ ์ด๋ค.
1. HashMap์ ์ด์ฉํด ๋ชจ์ค๋ถํธ์ ์ํ๋ฒณ์ ํค,๊ฐ ์์ผ๋ก ์ ์ฅํ๊ธฐ
2. split() ๋ฉ์๋๋ก ๋ฌธ์์ด์ ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๊ธฐ
3. StringBuilder๋ก ๋ฌธ์์ด์ ์กฐํฉํด์ ๋ฐํ
โญ 3. ์ ๋ต์ฝ๋
import java.util.*;
class Solution {
public String solution(String letter) {
Map<String, String> morse = new HashMap<>();
// ๋ชจ์ค ๋ถํธ ๋งคํ
morse.put(".-", "a"); morse.put("-...", "b");
morse.put("-.-.", "c"); morse.put("-..", "d");
morse.put(".", "e"); morse.put("..-.", "f");
morse.put("--.", "g"); morse.put("....", "h");
morse.put("..", "i"); morse.put(".---", "j");
morse.put("-.-", "k"); morse.put(".-..", "l");
morse.put("--", "m"); morse.put("-.", "n");
morse.put("---", "o"); morse.put(".--.", "p");
morse.put("--.-", "q"); morse.put(".-.", "r");
morse.put("...", "s"); morse.put("-", "t");
morse.put("..-", "u"); morse.put("...-", "v");
morse.put(".--", "w"); morse.put("-..-", "x");
morse.put("-.--", "y"); morse.put("--..", "z");
// ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๊ธฐ
String[] words = letter.split(" ");
// ๋ฌธ์ ์กฐํฉ
StringBuilder sb = new StringBuilder();
for (String word : words ) {
sb.append(morse.get(word));
}
return sb.toString();
}
}
๐๐ป ๋ ๊ฐ๋จํ ์ฝ๋
import java.util.*;
public class MorseDecoder {
public static String solution(String letter) {
Map<String, String> morse = Map.ofEntries(
Map.entry(".-", "a"), Map.entry("-...", "b"),
Map.entry("-.-.", "c"), Map.entry("-..", "d"),
Map.entry(".", "e"), Map.entry("..-.", "f"),
Map.entry("--.", "g"), Map.entry("....", "h"),
Map.entry("..", "i"), Map.entry(".---", "j"),
Map.entry("-.-", "k"), Map.entry(".-..", "l"),
Map.entry("--", "m"), Map.entry("-.", "n"),
Map.entry("---", "o"), Map.entry(".--.", "p"),
Map.entry("--.-", "q"), Map.entry(".-.", "r"),
Map.entry("...", "s"), Map.entry("-", "t"),
Map.entry("..-", "u"), Map.entry("...-", "v"),
Map.entry(".--", "w"), Map.entry("-..-", "x"),
Map.entry("-.--", "y"), Map.entry("--..", "z")
);
return Arrays.stream(letter.split(" "))
.map(morse::get)
.reduce("", String::concat);
}
public static void main(String[] args) {
System.out.println(solution(".... . .-.. .-.. ---")); // hello
}
}
๋ด๊ฐ ํผ ์ฝ๋๋ฅผ ์ด๋ ๊ฒ ํ ์ค๋ก ์ธ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
return Arrays.stream(letter.split(" ")).map(morse::get).reduce("", String::concat);
- `letter.split(" ")` ๋ชจ์ค๋ถํธ ๋ฌธ์์ด์ ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๊ธฐ
- `Arrays.stream(...)` ๋ฐฐ์ด์ ์คํธ๋ฆผ(Stream)์ผ๋ก ๋ณํํ๊ธฐ
- `.map(morse::get)` ๊ฐ ๋ชจ์ค๋ถํธ๋ฅผ ์์ด ์ํ๋ฒณ์ผ๋ก ๋ณํํ๊ธฐ
- `.reduce("", String::concat)` ๋ณํ๋ ์ํ๋ฒณ๋ค์ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ
'Algorithm > JAVAํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ 1. ๋ฌธ์ ์ค๋ช

์ ์ถ๋ ฅ ์
letter | result |
".... . .-.. .-.. ---" | "hello" |
".--. -.-- - .... --- -." | "python" |
๐ก 2. ์ ๊ทผ๋ฐฉ์
์ฃผ์ด์ง ๋ชจ์ค๋ถํธ๋ฅผ morse.put() ์ผ๋ก ํ ์ค์ฉ ํด์๋งต์ ์ถ๊ฐ ํด์ผ ํ๋ค๋ ๋ถ๋ถ์ด ๊ท์ฐฎ์ ๋ฌธ์ ์ด๋ค.
1. HashMap์ ์ด์ฉํด ๋ชจ์ค๋ถํธ์ ์ํ๋ฒณ์ ํค,๊ฐ ์์ผ๋ก ์ ์ฅํ๊ธฐ
2. split() ๋ฉ์๋๋ก ๋ฌธ์์ด์ ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๊ธฐ
3. StringBuilder๋ก ๋ฌธ์์ด์ ์กฐํฉํด์ ๋ฐํ
โญ 3. ์ ๋ต์ฝ๋
import java.util.*; class Solution { public String solution(String letter) { Map<String, String> morse = new HashMap<>(); // ๋ชจ์ค ๋ถํธ ๋งคํ morse.put(".-", "a"); morse.put("-...", "b"); morse.put("-.-.", "c"); morse.put("-..", "d"); morse.put(".", "e"); morse.put("..-.", "f"); morse.put("--.", "g"); morse.put("....", "h"); morse.put("..", "i"); morse.put(".---", "j"); morse.put("-.-", "k"); morse.put(".-..", "l"); morse.put("--", "m"); morse.put("-.", "n"); morse.put("---", "o"); morse.put(".--.", "p"); morse.put("--.-", "q"); morse.put(".-.", "r"); morse.put("...", "s"); morse.put("-", "t"); morse.put("..-", "u"); morse.put("...-", "v"); morse.put(".--", "w"); morse.put("-..-", "x"); morse.put("-.--", "y"); morse.put("--..", "z"); // ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๊ธฐ String[] words = letter.split(" "); // ๋ฌธ์ ์กฐํฉ StringBuilder sb = new StringBuilder(); for (String word : words ) { sb.append(morse.get(word)); } return sb.toString(); } }
๐๐ป ๋ ๊ฐ๋จํ ์ฝ๋
import java.util.*; public class MorseDecoder { public static String solution(String letter) { Map<String, String> morse = Map.ofEntries( Map.entry(".-", "a"), Map.entry("-...", "b"), Map.entry("-.-.", "c"), Map.entry("-..", "d"), Map.entry(".", "e"), Map.entry("..-.", "f"), Map.entry("--.", "g"), Map.entry("....", "h"), Map.entry("..", "i"), Map.entry(".---", "j"), Map.entry("-.-", "k"), Map.entry(".-..", "l"), Map.entry("--", "m"), Map.entry("-.", "n"), Map.entry("---", "o"), Map.entry(".--.", "p"), Map.entry("--.-", "q"), Map.entry(".-.", "r"), Map.entry("...", "s"), Map.entry("-", "t"), Map.entry("..-", "u"), Map.entry("...-", "v"), Map.entry(".--", "w"), Map.entry("-..-", "x"), Map.entry("-.--", "y"), Map.entry("--..", "z") ); return Arrays.stream(letter.split(" ")) .map(morse::get) .reduce("", String::concat); } public static void main(String[] args) { System.out.println(solution(".... . .-.. .-.. ---")); // hello } }
๋ด๊ฐ ํผ ์ฝ๋๋ฅผ ์ด๋ ๊ฒ ํ ์ค๋ก ์ธ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
return Arrays.stream(letter.split(" ")).map(morse::get).reduce("", String::concat);
letter.split(" ")
๋ชจ์ค๋ถํธ ๋ฌธ์์ด์ ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๊ธฐArrays.stream(...)
๋ฐฐ์ด์ ์คํธ๋ฆผ(Stream)์ผ๋ก ๋ณํํ๊ธฐ.map(morse::get)
๊ฐ ๋ชจ์ค๋ถํธ๋ฅผ ์์ด ์ํ๋ฒณ์ผ๋ก ๋ณํํ๊ธฐ.reduce("", String::concat)
๋ณํ๋ ์ํ๋ฒณ๋ค์ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ