-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorseCodeSize.java
More file actions
24 lines (21 loc) · 1.03 KB
/
morseCodeSize.java
File metadata and controls
24 lines (21 loc) · 1.03 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
/*Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.*/
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] MORSE = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."};
HashSet<String> hs = new HashSet<String>();
for(String s:words)
{
StringBuilder codedWord = new StringBuilder();
for(char c : s.toCharArray())
{
codedWord.append(MORSE[c-'a']);
}
hs.add(codedWord.toString());
}
return hs.size();
}
}