|
| 1 | +import java.util.HashSet; |
| 2 | +import java.util.Set; |
| 3 | + |
| 4 | +public class UniqueMorseCodeWords { |
| 5 | + private final String[] morseCodeMapping = new String[] { |
| 6 | + ".-","-...","-.-.","-..",".","..-.", |
| 7 | + "--.","....","..",".---","-.-",".-..", |
| 8 | + "--","-.","---",".--.","--.-",".-.", |
| 9 | + "...","-","..-","...-",".--","-..-", |
| 10 | + "-.--","--.." |
| 11 | + }; |
| 12 | + |
| 13 | + public int uniqueMorseRepresentations(String[] words) { |
| 14 | + Set<String> transformations = new HashSet<>(); |
| 15 | + for (String word : words) { |
| 16 | + transformations.add(morseValue(word)); |
| 17 | + } |
| 18 | + return transformations.size(); |
| 19 | + } |
| 20 | + |
| 21 | + private String morseValue(String word) { |
| 22 | + StringBuilder result = new StringBuilder(); |
| 23 | + for (char character : word.toCharArray()) { |
| 24 | + result.append(morseValue(character)); |
| 25 | + } |
| 26 | + return result.toString(); |
| 27 | + } |
| 28 | + |
| 29 | + private String morseValue(char character) { |
| 30 | + return morseCodeMapping[character - 'a']; |
| 31 | + } |
| 32 | +} |
0 commit comments