LeetCode - 804. Unique Morse Code Words
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, "cba" 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 "--...--.". 加班之余水个题~ 利用一下set的唯一性,效率还挺高
class Solution {
public int uniqueMorseRepresentations(String[] words) {
if (words == null)
return 0;
String[] codes = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int len = words.length;
Set<String> set = new HashSet<>();
for (int i=0; i<len; i++) {
String word = words[i];
StringBuilder codeBuilder = new StringBuilder();
for (int j=0; j<word.length(); j++) {
codeBuilder.append(codes[word.charAt(j)-'a']);
}
set.add(codeBuilder.toString());
}
return set.size();
}
}
LeetCode - 804. Unique Morse Code Words的更多相关文章
- 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 ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
随机推荐
- 避免 Deepin 15.4 系统 界面卡顿、假死等现象:隐藏自带的“任务栏”,安装轻量级的“任务栏tint2”
使用 Deepin 过程中,发现当点击“任务栏”上面的按钮,尤其是右键单击,选择菜单时,界面很容易卡顿,再也动弹不了. 好吧,,,就不使用自带的“任务栏”了,换成一个轻量级的“任务栏tint2”, 1 ...
- [CentOS7]安装tomcat并开启自启动
安装jdk 1.CentOS 6.X 和 7.X 自带有OpenJDK runtime environment (openjdk).它是一个在linux上实现开源的Java 平台. yum searc ...
- Jsoup的简易使用示例
http://www.open-open.com/jsoup/parsing-a-document.htm 测试用网页 <!doctype html> <!-- http://jwc ...
- [PHP] 多表外连接性能测试及优化
原文:https://blog.csdn.net/tang_huan_11/article/details/41925639 版权声明:本文为博主原创文章,转载请附上博文链接!
- Spark2.2(三十九):如何根据appName监控spark任务,当任务不存在则启动(任务存在当超过多久没有活动状态则kill,等待下次启动)
业务需求 实现一个根据spark任务的appName来监控任务是否存在,及任务是否卡死的监控. 1)给定一个appName,根据appName从yarn application -list中验证任务是 ...
- MySQL关于根据日期查询数据的sql语句
查询在某段日期之间的数据: select * from 数据表 where 时间字段名 BETWEEN '2016-02-01' AND '2016-02-05' 查询往前3个月的数据: selec ...
- 命令 上传项目到git中
点击Clone or dowload会出现一个地址,copy这个地址备用. 接下来就到本地操作了,首先右键你的项目,如果你之前安装git成功的话,右键会出现两个新选项,分别为Git Gui Here, ...
- angularjs $$phase
https://segmentfault.com/q/1010000000738004/a-1020000000738812 $$phase 是 angluar 内部使用的状态标志位,用于标识当前是否 ...
- InfluxDB源码阅读之httpd服务
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 服务模块介绍 源码路径: github.com/influxda ...
- 听闻 kubernetes,快速了解一番
看到 各位 大厂都在用这个, 而本人最多是用yarn 做些ML的事情, 赶快了解一下, 先扫盲记录一下. 一.名称趣闻 kubernetes缩写为k8s, 阿哈 ,原来是:k8s,意思就是k后面 ...