Unique Morse Code Words
给一个字符串组, 问有能组成几组不同的莫斯码, 用set.
class Solution {
String[] dict = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
public int uniqueMorseRepresentations(String[] words) {
Set<String> set = new HashSet<>();
int res = 0;
for(String w : words){
String r = get(w);
if(set.contains(r))
continue;
res++;
set.add(r);
}
return res;
}
private String get(String s) {
StringBuilder sb = new StringBuilder();
for(char c : s.toCharArray()) {
sb.append(dict[c - 'a']);
}
return sb.toString();
}
}