public static void main(String[] args) {
uniqueMorseRepresentations(new String[]{"gin", "zen", "gig", "msg"});
}
public static int uniqueMorseRepresentations(String[] words){
Map<String,String> map = new HashMap<>();
for (int i = 0; i < words.length; i++) {
String str =tomoersi(words[i]);
if (map.get(str)==null){
map.put(str,str);
}
}
return map.size();
}
public static String tomoersi(String s){
String str="";
char[] chars=s.toCharArray();
for (int i = 0; i < chars.length; i++) {
switch (chars[i]){
case 'a':
str+=".-";
break;
case 'b':
str+="-...";
break;
case 'c':
str+="-.-.";
break;
case 'd':
str+="-..";
break;
case 'e':
str+=".";
break;
case 'f':
str+="..-.";
break;
case 'g':
str+="--.";
break;
case 'h':
str+="....";
break;
case 'i':
str+="..";
break;
case 'j':
str+=".---";
break;
case 'k':
str+="-.-";
break;
case 'l':
str+=".-..";
break;
case 'm':
str+="--";
break;
case 'n':
str+="-.";
break;
case 'o':
str+="---";
break;
case 'p':
str+=".--.";
break;
case 'q':
str+="--.-";
break;
case 'r':
str+=".-.";
break;
case 's':
str+="...";
break;
case 't':
str+="-";
break;
case 'u':
str+="..-";
break;
case 'v':
str+="...-";
break;
case 'w':
str+=".--";
break;
case 'x':
str+="-..-";
break;
case 'y':
str+="-.--";
break;
case 'z':
str+="--..";
break;
}
}
return str;
}
唯一摩尔斯密码词(力扣题库)
最新推荐文章于 2020-08-26 16:28:25 发布