forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode804.java
More file actions
25 lines (21 loc) · 808 Bytes
/
LeetCode804.java
File metadata and controls
25 lines (21 loc) · 808 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
package LeetCode;
import java.util.HashSet;
import java.util.Set;
class LeetCode804 {
private String[] morse = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
public int uniqueMorseRepresentations(String[] words) {
Set<String> set = new HashSet<>();
for (String word : words) {
set.add(getMorse(word));
}
return set.size();
}
// 将每个字符翻译成摩斯密码
private String getMorse(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
sb.append(morse[str.charAt(i) - 'a']);
}
return sb.toString();
}
}