【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words
Description
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations, "--...-." and "--...--.".
Note
- The length of words will be at most 100.
- Each words[i] will have length in range [1, 12].
- words[i] will only consist of lowercase letters.
Discuss
把字母对应的字符串存起来,一次遍历每一个单词,然后去重就可以了。
Code
class Solution {
public int uniqueMorseRepresentations(String[] words) {
List<String> list = Arrays.asList(".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..",
"--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..");
Map<Integer, String> map = new HashMap<>();
int ans = 0;
for (int i = 97; i <= 122; i++) {
map.put(i, list.get(ans++));
}
Set<String> set = new HashSet<>();
for (int i = 0; i < words.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < words[i].length(); j++) {
sb.append(map.get(Integer.valueOf(words[i].charAt(j))));
}
set.add(sb.toString());
}
return set.size();
}
}
【Leetcode】804. Unique Morse Code Words的更多相关文章
- 【LeetCode】804. Unique Morse Code Words 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)
题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...
- [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
随机推荐
- May 23rd 2017 Week 21st Tuesday
Winners are not those who never fail but those who never quit. 成功者不是从不失败,而是从不放弃. Nothing is impossib ...
- March 2 2017 Week 9 Thursday
The first duty of love is to listen. 爱的首要责任是倾听. Yesterday, I read an article that says a successful ...
- Spark Streamming 基本输入流I(-) :File/Hdfs
Spark Streamming 基本输入流I(-):从文件中进行读取 文件读取1:本地文件读取 这里我只给出实现代码及操作步骤 1.在本地目录下创建目录,这里我们创建目录为~/log/ 2.然后手动 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- focal loss和retinanet
这个是自己用的focal loss的代码和公式推导:https://github.com/zimenglan-sysu-512/Focal-Loss 这个是有retinanet:https://git ...
- git bush 代码提交
# git add . # git commit -m"init project" # git push
- phpstorm常用plugins
CodeGlance JsonOnlineViewer CSS-X-Fire Laravel Plugin PHP annotations
- Python条件判断
计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= 18: p ...
- Promise面试题
题目一 const promise = new Promise((resolve, reject) => { console.log(1); resolve(); console.log(2); ...
- SQL Server笔记——sql语句创建数据库
MS SQLServer的每个数据库包含: 1个主数据文件(.mdf)必须. 1个事务日志文件(.ldf)必须. 可以包含: 任意多个次要数据文件(.ndf) 多个事务日志文件 CREATE DATA ...