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.

 
 
Though every alphabet represents different Morse Code, some of the combinations will be the same. To solve this problem, the first step is that we need to change all the strings to their Morse Codes. The second step is to store the code in a list. If the code is already in the list, we do not store it. After the second step, we just need to count the lenght of the code list. Then the lenght is the answer.
 
class Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
sta=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] w=[] for s in words:
a=''
for c in s:
a=a+sta[ord(c)-ord('a')]
if not a in w:
w.append(a)
return len(w)

  

 

[LeetCode&Python] Problem 804. Unique Morse Code Words的更多相关文章

  1. 【Leetcode_easy】804. Unique Morse Code Words

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

  2. 804. Unique Morse Code Words - LeetCode

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

  3. 【Leetcode】804. Unique Morse Code Words

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

  4. 【LeetCode】804. Unique Morse Code Words 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...

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

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

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

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

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

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

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

随机推荐

  1. Centos修改系统语言

    使用man page帮助时,发现居然是中文的,不过想想即便英语再水,也要逼着自己去适应.于是百度找了一下修改系统语言的方法. 首先使用 locale 命令查看当前的系统语言 然后修改时一般有两种方法, ...

  2. 【Golang】解决Go test执行单个测试文件提示未定义问题

    背景 很多人记录过怎么执行Go test单个文件或者单个函数,但是要么对执行单文件用例存在函数或变量引用的场景避而不谈,要么提示调用了其它文件中的模块会报错.其实了解了go test命令的机制之后,这 ...

  3. 【Mac】小技巧:实现ssh服务器别名免密登录

    前言 我们平常使用ssh user@host然后输入密码的方式来远程链接一个服务器,但是,如果要管理的服务器太多,记住这些服务器的IP和用户名.密码就是一个复杂的工作.当然,我们可以把这些信息用文档记 ...

  4. unity中实现静态的3D对象对其他对象的跟随

    using UnityEngine; public class FollowPosition : MonoBehaviour { public Transform targetTrans; publi ...

  5. spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获

    spring boot: GlobalDefaultExceptionHandler方法内的友好错误提示,全局异常捕获 当你的某个控制器内的某个方法报错,基本上回显示出java错误代码,非常不友好,这 ...

  6. jsp动作之 setProperty

    setProperty用来设置useBean实例的属性. 如useBean实例化了一个类,类中有nickname属性,那么,我们可以用setProperty来重新定义他的值. setProperty有 ...

  7. [Java学习] Java包装类、拆箱和装箱详解

    虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...

  8. [Java代码] Java是自学好还是参加培训班好?

    ava 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统. 本教程给大家简单介 ...

  9. java concurrent包的实现原理

      由于java的CAS同时具有 volatile 读和volatile写的内存语义,因此Java线程之间的通信现在有了下面四种方式:   A线程写volatile变量,随后B线程读这个volatil ...

  10. A Creative Cutout CodeForces - 933D (计数)

    大意:给定$n$个圆, 圆心均在原点, 第$k$个圆半径为$\sqrt{k}$ 定义一个点的美丽值为所有包含这个点的圆的编号和 定义函数$f(n)$为只有$n$个圆时所有点的贡献,求$\sum_{k= ...