【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 ...
随机推荐
- Selenium2学习(十四)-- 加载Firefox配置
前言有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_ ...
- PS软件怎么把视频转成gif动态图?
PS软件怎么把视频转成gif动态图?Adobe PhotoShop软件的最新版本是可以编辑视频的,并且可以将视频转换为GIF动态图,使用也很简单,下面分享ps制作gif动态图的教程,需要的朋友可以参考 ...
- 别跑了,看看脚下有木有money
别跑了,看看脚下有木有money 1.长大论坛 本来是想做个博客的,无奈级别不够,博客过于复杂,所以就做了这个类似于贴吧的东西.在这里,你可以发帖:可以吐槽学校.可以向他人寻求帮助.甚至可以 ...
- 使flex-direction: column的子元素height: 100%生效的办法
在flex-direction: column子元素里直接使用height:100%,height并不会被设置成100% <!DOCTYPE html> <html lang=&qu ...
- (转)Wireshark基本介绍和学习TCP三次握手
原地址https://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html#filter 阅读目录 wireshark介绍 wireshar ...
- POJ-3484 Showstopper---二分+前缀和
题目链接: https://cn.vjudge.net/problem/POJ-3484 题目大意: 给出一系列等差数列,给出第一项和最后一项和公差 这些等差数列中每个数出现的次数只有一个是奇数,找出 ...
- Android进阶笔记12:ListView篇之图片优化
1.图片异步加载: (1)处理图片的方式: 如果ListView中自定义的Item中有涉及到大量图片的,一定要对图片进行细心的处理,因为图片占的内存是 ListView 项中最头疼的,处理图片的方法大 ...
- Python语言程序设计基础(2)—— Python程序实例解析
温度转换 def tempConvert(ValueStr): if ValueStr[-1] in ['F','f']: ans = (eval(ValueStr[0:-1]) - 32)/1.8 ...
- 【[USACO16OPEN]262144】
发现这个数列的范围特别大但是值域的范围特别小 于是可以大胆猜测这道题值域肯定需要开到状态里去 又发现\(262144=2^{18}\)这个暗示非常明显啊,暗示这道题跟二进制有关系 其实也没什么关系 设 ...
- 【[AHOI2012]树屋阶梯】
卡特兰数! 至于为什么是卡特兰数,就稍微说那么一两句吧 对于一个高度为\(i\)的阶梯,我们可以在左上角填一个高度为\(k\)的阶梯,右下角填一个高度为\(i-1-k\)的阶梯剩下的我们用一个大的长方 ...