1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public int uniqueMorseRepresentations(String[] words) { String[] morseCode = new String[] { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; Set<String> transformationSet = new HashSet<>(); for (String word : words) { StringBuilder currTransformation = new StringBuilder(); for (char c : word.toCharArray()) { currTransformation.append(morseCode[c - 'a']); } transformationSet.add(currTransformation.toString()); } return transformationSet.size(); } }
|