[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, "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 most100
. - Each
words[i]
will have length in range[1, 12]
. words[i]
will only consist of lowercase letters.
给定了26字母的摩斯电码的编码,给一组单词,把每个单词都转成摩斯码,返回有多少个不同的摩斯码。
解法:题目很简单,直接转换判断即可。
Java:
public int uniqueMorseRepresentations(String[] words) {
String[] d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
HashSet<String> s = new HashSet<>();
for (String word : words) {
String code = "";
for (char c : word.toCharArray()) code += d[c - 'a'];
s.add(code);
}
return s.size();
}
Python:
def uniqueMorseRepresentations(self, words):
d = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
return len({''.join(d[ord(i) - ord('a')] for i in w) for w in words})
Python:
# Time: O(n), n is the sume of all word lengths
# Space: O(n) class Solution(object):
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
MORSE = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--.."] lookup = {"".join(MORSE[ord(c) - ord('a')] for c in word) \
for word in words}
return len(lookup)
Python: wo
class Solution(object):
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
m = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
trans = []
res = 0
for word in words:
temp = ''
for c in word:
temp += m[ord(c) - 97]
if temp not in trans:
trans.append(temp)
res += 1 return res
C++:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
unordered_set<string> s;
for (auto word : words) {
string code;
for (auto c : word) code += d[c - 'a'];
s.insert(code);
}
return s.size();
}
C++:
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> morse{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
unordered_set<string> s;
for (string word : words) {
string t = "";
for (char c : word) t += morse[c - 'a'];
s.insert(t);
}
return s.size();
}
};
假定followup: 给一个单词的摩斯码,问有几种可能的单词,比如:"--...-.",至少有两种zen和gin
All LeetCode Questions List 题目汇总
最新文章
- thinkphp 验证
- The C Programming Language (second edition) 实践代码(置于此以作备份)
- 如何创建Asp.net MVC ViewModel
- 1195: [HNOI2006]最短母串 - BZOJ
- chkdsk
- 分享一款在线less转css的神器
- git 免密码push
- zTree:一个依靠 jQuery 实现的多功能 “树插件”
- Vue(十七)模块化开发
- [转]你可能不知道的五个强大HTML5 API
- MySQL 删除重复数据实例
- 多线程系列(2)线程池ThreadPool
- net自定义安装程序快捷方式
- 2 BeeGo 参数配置与路由配置
- 爬豆瓣影评,记下解决maximum recursion depth exceeded in cmp
- nodeJs学习过程之认识nodejs
- html to openxml
- $.when()方法监控ajax请求获取到的数据与普通ajax请求回调获取到的数据的不同
- 没有为扩展名“.cshtml”注册的生成提供程序。
- 游戏动作师使用Unity3D遇到过的所有问题
热门文章
- python笔记39-unittest框架如何将上个接口的返回结果给下个接口适用(面试必问)
- Echo团队Alpha冲刺 - 测试随笔
- less-4
- go 学习 (三):函数 &; 指针 &; 结构体
- PostgreSQL JSON 处理
- BZOJ 3553: [Shoi2014]三叉神经树 LCT
- pgloader 学习(五)pgloader 参考手册
- 洛谷 题解 UVA572 【油田 Oil Deposits】
- ThinkPad T410i 2516A21 升級手札(換SSD固態硬碟、I7 CPU、開機20秒)
- MYSQL 什么时候用单列索引?什么使用用联合索引?