-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
29 lines (25 loc) · 879 Bytes
/
Solution.java
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
/**
* User: Changle
* Date: 2019-03-01 11:29:16
* Source: https://leetcode.com/problems/unique-morse-code-words/
*/
class Solution {
final String[] morseCodes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--.."};
public int uniqueMorseRepresentations(String[] words) {
Set<String> set = new HashSet<String>();
for (String word : words) {
// 解析成莫斯密码
set.add(trans2MorseCode(word));
}
return set.size();
}
private String trans2MorseCode(String word) {
String result = "";
for (int i = 0; i < word.length(); i++) {
result += morseCodes[word.charAt(i) - 'a'];
}
return result;
}
}