[코테 풀이] Unique Morse Code Words

시내·2024년 6월 17일
0

Q_804) Unique Morse Code Words

출처 : https://leetcode.com/problems/unique-morse-code-words/

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
         Map<Character, String> map = new HashMap<>();
        String[] given = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
                "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
        char a = 'a';
        int ind = 0;
        while (a <= 'z') {
            map.put(a, given[ind++]);
            a++;
        }
        Set<String> set = new HashSet<>(); //duplicates not allowed
        for (int i = 0; i < words.length; i++) {
            String concat = "";
            for (int j = 0; j < words[i].length(); j++) {
                if (map.containsKey(words[i].charAt(j))) {
                    concat += map.get(words[i].charAt(j));
                }
            }
            set.add(concat);
        }
        return set.size();
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글