본문 바로가기
Problem Solving/LeetCode

[LeetCode] 804. Unique Morse Code Words - Java

by graycode 2026. 10. 16.

• 문제 링크

 

Unique Morse Code Words - LeetCode

Can you solve this real interview question? Unique Morse Code Words - International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: * 'a' maps to ".-", * 'b' maps to "-...", * 'c' maps to "-.-.

leetcode.com

 

• 풀이 코드

import java.util.HashSet;
import java.util.Set;

public class Solution {

    public int uniqueMorseRepresentations(String[] words) {
        String[] arr = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
        Set<String> set = new HashSet<>();
        for (String s : words) {
            StringBuilder sb = new StringBuilder();
            for (char c : s.toCharArray()) sb.append(arr[c - 'a']);
            set.add(sb.toString());
        }

        return set.size();
    }

}

댓글