Post

[Programmers] #120838 - 모스부호 (1) [Java][C++][Python]

[Programmers] #120838 - 모스부호 (1) [Java][C++][Python]

문제 링크


1. 아이디어

공백으로 구분된 모스부호 문자열을 영어 소문자 문자열로 해독하는 문제다. 부호 26개가 알파벳 26자와 일대일로 대응하고, 부호 사이의 공백이 연속하지 않으며 양 끝에도 없으므로 공백으로 나눈 조각이 곧 부호 하나씩이다. 따라서 부호에서 문자로 가는 대응표를 만들어 두고, 조각마다 조회해 이어 붙이면 된다.


2. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

class Solution {
    public 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")
        );

        StringBuilder sb = new StringBuilder();
        for (String s : letter.split(" ")) {
            sb.append(morse.get(s));
        }

        return sb.toString();
    }
}

Map.of는 10쌍까지만 받아 26쌍은 Map.ofEntries로 만들었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

string solution(string letter) {
    unordered_map<string, char> 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'}};

    stringstream ss(letter);
    string ans;
    string code;
    while (ss >> code) {
        ans += morse[code];
    }

    return ans;
}

std::stringstream에서 >>로 읽어 공백을 기준으로 부호를 하나씩 얻었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def solution(letter):
    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",
    }

    return "".join(morse[code] for code in letter.split())

This post is licensed under CC BY 4.0 by the author.