-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL804.java
More file actions
31 lines (29 loc) · 1.47 KB
/
L804.java
File metadata and controls
31 lines (29 loc) · 1.47 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
import java.util.HashSet;
import java.util.Set;
class Solution804 {
class Solution {
private final String[] MORSE = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
/**
* 804. Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/description/
*
* @param words String[]
* The list of input words to be transformed to Morse
* @return The number of unique morse transformations
* @timeComplexity O(n) where n is the number of chars in all words
* @spaceComplexity O(n) where n is the number of chars in all unique transformations
*/
public int uniqueMorseRepresentations(String[] words) {
Set<String> transformations = new HashSet<>();
for (String word : words) {
StringBuffer sb = new StringBuffer();
for (char c : word.toCharArray()) {
sb.append(MORSE[c - 'a']);
}
transformations.add(sb.toString());
}
return transformations.size();
// Alternate and slower one liner accepted solution
// Arrays.asList(words).stream().map(word -> word.chars().mapToObj(c -> MORSE[c - 'a']).collect(Collectors.joining())).collect(Collectors.toSet()).size();*/
}
}
}