-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path804.java
More file actions
58 lines (57 loc) · 2.23 KB
/
804.java
File metadata and controls
58 lines (57 loc) · 2.23 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] translate = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
HashSet<String> set = new HashSet<>();
for (String word: words) {
StringBuilder code = new StringBuilder();
for (char c: word.toCharArray()) {
String morse = translate[c - 'a'];
code.append(morse);
}
set.add(code.toString());
}
return set.size();
}
}
__________________________________________________________________________________________________
sample 35368 kb submission
class Solution {
public int uniqueMorseRepresentations(String[] words) {
if(words.length == 1){
return 1;
}
if(words.length == 0){
return 0;
}
String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
String[] morses = new String[words.length];
for(int i = 0; i < words.length; i++){
String word = "";
for(int j = 0; j < words[i].length(); j++){
int letter = ((int)words[i].charAt(j)) - 97;
word += morse[letter];
}
morses[i] = word;
}
int result = 0;
for(int i = 0; i < morses.length; i++){
if(morses[i] != null){
result++;
}
if(morses[i] != null && i != morses.length - 1){
for(int j = i + 1; j < morses.length; j++){
if(morses[i].equals(morses[j])){
morses[j] = null;
}
}
}
}
for(int i = 0; i < 5000; i++){
System.out.println("useless");
}
return result;
}
}
__________________________________________________________________________________________________