-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathUniqueMorseCodeWords.java
More file actions
32 lines (20 loc) · 846 Bytes
/
UniqueMorseCodeWords.java
File metadata and controls
32 lines (20 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package leetcode;
import java.util.HashSet;
/**
* Created by nikoo28 on 9/22/18 5:25 PM
*/
class UniqueMorseCodeWords {
public int uniqueMorseCodeRepresentations(String[] words) {
String[] morseCode = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
StringBuilder morseRepresentation = new StringBuilder();
HashSet<String> uniqueRepresentations = new HashSet<>();
for (String word : words) {
morseRepresentation.setLength(0);
for (int i = 0; i < word.length(); i++) {
morseRepresentation.append(morseCode[word.charAt(i) - 'a']);
}
uniqueRepresentations.add(morseRepresentation.toString());
}
return uniqueRepresentations.size();
}
}