【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 ...
随机推荐
- 模拟精灵 z
反复做历史测试,重大改进了卖出的判断模式.此项改进能使系统收益每年增加5%-左右 重新整合了几种买法,使之在熊市更加谨慎.对大盘的反转反应更为灵敏 适当加大了仓位 单独处理有重大机会的股票 加入多种短 ...
- js数据封装处理
var arr = [ { id: "1", date: "2018-07-27", time: "10:00-12:00", schedu ...
- 【转载】#446 - Deciding Between an Abstract Class and an Interface
An abstract class is a base class that may have some members not implemented in the base class, but ...
- swift 协议(结合扩展)的特点
协议的传统实现: 定义接口+实现协议 由抽象到具体: 协议的逆向实现(使用扩展): 由已存在的类型抽离部分功能作为协议,并让原体符合协议: 由具体到抽象: 向上抽离: 向上生成: 协议的缺省 ...
- 【转】Android 之ActivityThead、ActivityManagerService 与activity的管理和创建
在android中,Activity是四大组件中比较重要的一个(当然其他的也比较重要),那么android中是怎样管理这些activity的?应用的进程和主线程是怎么创建的,应用的消息循环又是在什么时 ...
- Node.js使用MySQL数据库中对RowDataPacket对象的使用
使用Node.js开发使用MySQL数据库的网站,在查询后返回一RowDataPacket类型的对象 原先使用toString()方法一直得到仅为object的字符串,无法使用 后思考,才发现忽略了其 ...
- Spring data jpa命名规范
JPA命名规范 (sample与JPQL等效) Table 4. Supported keywords inside method names Keyword Sample JPQL snippet ...
- 使用vue搭建项目(创建手脚架)
第一步:切换到创建的目录 创建项目 vue cerate [model] 第二步:切换到创建好的项目,然后创建element vue add element 第三步:创建router vue add ...
- 【洛谷P4124】[CQOI2016]手机号码
手机号码 数位DP模板题 记忆化搜索: #include<iostream> #include<cstring> #include<cstdio> using na ...
- 【luogu P3371 单源最短路】 模板 vector+SPFA
stl真是好,,偷懒少写邻接表,, 两个STL应用使代码简短了很多.然而还是那句话,天上不会掉馅饼,程序的效率还是有所下降的.然而,效率不是全部,人们宁可牺牲三倍效率用Java而不用C语言就是最好的例 ...