import java.util.HashSet;
import java.util.Set;
/**
* @author xnl
* @Description:
* @date: 2022/6/22 22:06
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
String[] words = {"gin", "zen", "gig", "msg"};
System.out.println(solution.uniqueMorseRepresentations(words));
}
public int uniqueMorseRepresentations(String[] words) {
Set<String> list = new HashSet<>();
String[] table = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
for (String word : words) {
String str = "";
for (char c : word.toCharArray()) {
str += table[c - 'a'];
}
list.add(str);
}
return list.size();
}
}
力扣:804. 唯一摩尔斯密码词
最新推荐文章于 2025-12-06 07:43:37 发布
该博客展示了一个 Java 程序,用于计算给定单词列表在 Morse 码表示下的唯一组合数量。程序创建了一个 Morse 码表并遍历单词,将每个字符转换为 Morse 码字符串,最后利用 HashSet 去除重复项并返回唯一表示的个数。
621

被折叠的 条评论
为什么被折叠?



