804. Unique Morse Code Words

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] dotAndDash = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        Set<String> set = new HashSet<String>();
        for (String word: words) {
            StringBuilder concatenation = new StringBuilder();
            for (char c: word.toCharArray()) {
                concatenation.append(dotAndDash[c-97]);
            }
            set.add(concatenation.toString());
        }
        int result = set.size();
        System.out.println(result);
        return result;
    }
}

 

posted @ 2019-05-29 21:20  stoneBlog  阅读(87)  评论(0编辑  收藏  举报