-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (25 loc) · 903 Bytes
/
Solution.java
File metadata and controls
27 lines (25 loc) · 903 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
package _804;
import java.util.HashSet;
public class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] codeArray = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
HashSet<Long> set = new HashSet<>();
int ans = 0;
for (String s : words) {
long key = 0;
for (char c : s.toCharArray()) {
String code = codeArray[c - 'a'];
for (char digit : code.toCharArray()) {
key <<= 1;
key |= (digit == '.' ? 0 : 1);
}
}
if (!set.contains(key)) {
set.add(key);
ans++;
}
}
return ans;
}
}