解题思路:
(1)建一个HashMap,将26个英文字母映射成摩尔斯密码
(2)遍历每个单词,将每个单词中的字符翻译成对应的摩尔斯密码
(3)再创建一个HashMap,用来存储单词映射成的摩尔斯密码,如果,这个摩尔斯密码字符串已经出现过,则跳过,若未出现过,则将其存入HashMap,并将计数器加1
class Solution {public int uniqueMorseRepresentations(String[] words) {Map<String,String> maps=new HashMap<String,String>();maps.put("a",".-");maps.put("b","-...");maps.put("c","-.-.");maps.put("d","-..");maps.put("e",".");maps.put("f","..-.");maps.put("g","--.");maps.put("h","....");maps.put("i","..");maps.put("j",".---");maps.put("k","-.-");maps.put("l",".-..");maps.put("m","--");maps.put("n","-.");maps.put("o","---");maps.put("p",".--.");maps.put("q","--.-");maps.put("r",".-.");maps.put("s","...");maps.put("t","-");maps.put("u","..-");maps.put("v","...-");maps.put("w",".--");maps.put("x","-..-");maps.put("y","-.--");maps.put("z","--..");int len=words.length;int cnt=0;Map<String,Integer> resMap=new HashMap<String,Integer>();for(int i=0;i<len;++i){String temp="";for(int j=0;j<words[i].length();++j){temp+=maps.get(String.valueOf(words[i].charAt(j)));}if(resMap.containsKey(temp)){continue;}else{cnt++;resMap.put(temp,1);}}return cnt;}
}