版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qfashly/article/details/79883802
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: “a” 对应 “.-“, “b” 对应 “-…”, “c” 对应 “-.-.”, 等等。
为了方便,所有26个英文字母对应摩尔斯密码表如下:
[“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—”,”-.-“,”.-..”,”–”,”-.”,”—”,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–”,”-..-“,”-.–”,”–..”]
public static int uniqueMorseRepresentations(String[] words) {
if(words == null || words.length <= 0){
return 0;
}
if(words.length == 1){
return 1;
}
for(String word : words){
word = word.toLowerCase();
}
Map<String, String> map = new HashMap<String, String>();
for(String word : words){
word = words2Morse(word);
map.put(word, word);
}
return map.size();
}
public static String words2Morse(String word){
if(word == null || word == ""){
return "";
}
word = word.replace("a", ".-").replace("b", "-...").replace("c", "-.-.").replace("d", "-..").replace("e", ".")
.replace("f", "..-.").replace("g", "--.").replace("h", "....").replace("i", "..").replace("j", ".---")
.replace("k", "-.-").replace("l", ".-..").replace("m", "--").replace("n", "-.").replace("o", "---")
.replace("p", ".--.").replace("q", "--.-").replace("r", ".-.").replace("s", "...").replace("t", "-")
.replace("u", "..-").replace("v", "...-").replace("w", ".--").replace("x", "-..-").replace("y", "-.--").replace("z", "--..");
return word;
}