import java.util.TreeSet;
public class Algorithm {
public static void main(String[] args) {
String[] words = {"gin", "zen", "gig", "msg"};
System.out.println(new Solution().uniqueMorseRepresentations(words));
}
}
class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
TreeSet<String> set = new TreeSet<>();
for (String word : words) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
str.append(codes[word.charAt(i) - 'a']);
}
set.add(str.toString());
}
return set.size();
}
}
https://leetcode-cn.com/problems/unique-morse-code-words/