804. 唯一摩尔斯密码词

import java.util.TreeSet;

public class Algorithm {

    public static void main(String[] args) {

        String[] words = {"gin", "zen", "gig", "msg"};

        System.out.println(new Solution().uniqueMorseRepresentations(words));
    }
}

class Solution {
    public int uniqueMorseRepresentations(String[] words) {

        String[] codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

        TreeSet<String> set = new TreeSet<>();

        for (String word : words) {

            StringBuilder str = new StringBuilder();

            for (int i = 0; i < word.length(); i++) {

                str.append(codes[word.charAt(i) - 'a']);
            }

            set.add(str.toString());
        }

        return set.size();
    }
}

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

posted @ 2021-10-28 21:33  振袖秋枫问红叶  阅读(45)  评论(0)    收藏  举报