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.

这道题说的就是大名鼎鼎的摩斯码了,给了我们所有字母的摩斯码的写法,然后给了我们一个单词数组,问我们表示这些单词的摩斯码有多少种。因为某些单词的摩斯码表示是相同的,比如gin和zen就是相同的。最简单直接的方法就是我们求出每一个单词的摩斯码,然后将其放入一个HashSet中,利用其去重复的特性,从而实现题目的要求,最终HashSet中元素的个数即为所求,参见代码如下:

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();
}
};

讨论:这道题其实没有充分发挥其潜力,摩斯码的场景很好,只是作为一道Easy题未免有些可惜了。一个比较显而易见的follow up就是,给我们一个摩斯码,问其有几种可能的单词组,比如给我们一个"--...-.",那么我们知道至少有两种zen和gin,可能还有更多,这样是不是就更加有趣了呢?

类似题目:

https://leetcode.com/problems/unique-morse-code-words/solution/

https://leetcode.com/problems/unique-morse-code-words/discuss/120675/C++JavaPython-Easy-and-Concise-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  1. [LeetCode] 804. 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

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  4. 804. Unique Morse Code Words - LeetCode

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

  5. Unique Morse Code Words

    Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...

  6. 【Leetcode_easy】804. Unique Morse Code Words

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

  7. LeetCode804. Unique Morse Code Words

    题目 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 ...

  8. uva 508 - Morse Mismatches(摩斯码)

    来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...

  9. Javascript实现摩斯码加密解密

    原文地址 作者:liaoyu 摩尔斯电码是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号,是由美国人萨缪尔·摩尔斯在1836年发明. 每一个字符(字母或数字)对应不同的 ...

随机推荐

  1. Scrapy 入门

    Scrapy https://docs.scrapy.org/en/latest/intro/overview.html Scrapy is an application framework for ...

  2. NTFS权限笔记 2017-12-4

    NTFS权限:(文件或文件夹右键属性--安全--ACL) 1.文件系统类型: NTFS:支持单个文件大于4个G,支持文件权限设置 FAT32:不支持单个文件大于4G,不支持文件权限设置 2.取消权限继 ...

  3. 程序设计-理解java继承-遁地龙卷风

    (0)写在前面 编程和现实世界是息息相关的,你是如何理解现实世界中的继承呢? 继承在编程中应理解为:对父类资源(本文只讨论方法)的使用,父类方法只提供类基本的功能,父类方法之间不存在调用关系. (1) ...

  4. Java 计算两个日期相差月数、天数

    package com.myjava; import java.text.ParseException; import java.text.SimpleDateFormat; import java. ...

  5. mybatis(入门级项目)

    框架的搭建:(两个java类,两个xml配置文件) 1.导入jar包,日志debug文件以及数据库的参数文件 2.建立持久化类(和数据库的列值相同的类) user类的一个扩展类: userQueryV ...

  6. 🍓 DOM常用基础知识点汇总(入门者适用) 🍓

    铛-今天又没啥事,来总结一下DOM的基础知识.(公司没活干我也很无奈

  7. sublim 插件

    sublim 插件 https://www.cnblogs.com/hykun/p/sublimeText3.html html 代码自动 + tab ul>li>img+p+a ! ul ...

  8. poj 3278 搜索

    描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediate ...

  9. UiAutomator1.0 与 UiAutomator2.0

      在使用2.0之前,对android自动化框架也做过一些了解<Android 自动化测试框架>.使用UiAutomator2.0也有一段时间,这里将1.0与2.0进行一个对比总结. Ui ...

  10. C/C++中const关键字的用法及其与宏常量的比较

    1.const关键字的性质 简单来说:const关键字修饰的变量具有常属性. 即它所修饰的变量不能被修改. 2.修饰局部变量 ; ; 这两种写法是等价的,都是表示变量的值不能被改变,需要注意的是,用c ...