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.

给定了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 题目汇总

最新文章

  1. thinkphp 验证
  2. The C Programming Language (second edition) 实践代码(置于此以作备份)
  3. 如何创建Asp.net MVC ViewModel
  4. 1195: [HNOI2006]最短母串 - BZOJ
  5. chkdsk
  6. 分享一款在线less转css的神器
  7. git 免密码push
  8. zTree:一个依靠 jQuery 实现的多功能 “树插件”
  9. Vue(十七)模块化开发
  10. [转]你可能不知道的五个强大HTML5 API
  11. MySQL 删除重复数据实例
  12. 多线程系列(2)线程池ThreadPool
  13. net自定义安装程序快捷方式
  14. 2 BeeGo 参数配置与路由配置
  15. 爬豆瓣影评,记下解决maximum recursion depth exceeded in cmp
  16. nodeJs学习过程之认识nodejs
  17. html to openxml
  18. $.when()方法监控ajax请求获取到的数据与普通ajax请求回调获取到的数据的不同
  19. 没有为扩展名“.cshtml”注册的生成提供程序。
  20. 游戏动作师使用Unity3D遇到过的所有问题

热门文章

  1. python笔记39-unittest框架如何将上个接口的返回结果给下个接口适用(面试必问)
  2. Echo团队Alpha冲刺 - 测试随笔
  3. less-4
  4. go 学习 (三):函数 &amp; 指针 &amp; 结构体
  5. PostgreSQL JSON 处理
  6. BZOJ 3553: [Shoi2014]三叉神经树 LCT
  7. pgloader 学习(五)pgloader 参考手册
  8. 洛谷 题解 UVA572 【油田 Oil Deposits】
  9. ThinkPad T410i 2516A21 升級手札(換SSD固態硬碟、I7 CPU、開機20秒)
  10. MYSQL 什么时候用单列索引?什么使用用联合索引?