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

[LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词的更多相关文章

  1. [LeetCode] Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  2. LeetCode 804 Unique Morse Code Words 解题报告

    题目要求 International Morse Code defines a standard encoding where each letter is mapped to a series of ...

  3. LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)

    题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...

  4. Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

    参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...

  5. (string 数组) leetcode 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  6. LeetCode - 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  7. [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 ...

  8. 804. Unique Morse Code Words - LeetCode

    Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...

  9. 【Leetcode_easy】804. Unique Morse Code Words

    problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...

随机推荐

  1. java基础(14)---修饰符

    修饰符:final .static.public.protected.private.default. 一.final(不能修改) 使用final修饰变量定义:该变量一旦被初始化之后就不允许再被修改. ...

  2. php中array的常用操作示码

    融会了,也就熟悉了. 这事得多练,多改. <?php $empty1 = []; $empty2 = array(); $names = ['Harry', 'Ron', 'Hermione'] ...

  3. 项目Alpha冲刺(团队)-总结篇

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 作业目标:描述项目预期计划.现实进展.过程体会.组员分工 ...

  4. vscode——如何对MarkDown文件进行预览

    前言 一般都是用Typora直接进行编写了,今天恰好在vs中写完代码,就需要编辑文档,这里就记录下如何预览吧 步骤 ctrl+shift+p打开命令面板,然后输入markdowm->选择在侧边打 ...

  5. Python中实现count(distinct )

    假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句: select c1,count(distinct(c6)) from tbl where c3>1 group by ...

  6. xml的运用

    <?xml version="1.0" encoding="utf-8"?><class> <student> <na ...

  7. Python3 - 基础(运算符)

    Python3-运算符 举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算 ...

  8. C++中的类所占内存空间总结(转)

    类所占内存的大小是由成员变量(静态变量除外)决定的,成员函数(这是笼统的说,后面会细说)是不计算在内的. 摘抄部分: 成员函数还是以一般的函数一样的存在.a.fun()是通过fun(a.this)来调 ...

  9. Git学习笔记--实践(三)

    文中红色的文字(标为:## 插曲)是在Git学习/实践过程中,我个人遇到的一些问题,每个“## 插曲”之后,都有相应的解决方案. 一.创建版本库 版本库又名仓库,英文名repository,可简单的理 ...

  10. web自动化测试-selenium的三种等待

    一.等待的作用 1.在系统的功能运行过程中,所有的内容是需要一定的时间来实现展示, 2.时间耗费长短与网络速度.系统框架设定.接口的执行复杂度有关, 3.因此需要设置缓冲时间,若未设置缓冲时间,容易导 ...