7. Unique Morse Code Words

Title:

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, "cba" 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 1:

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.

Analysis of Title:

It meas to connect the words intto the Morse Code, and return the number of defferent transformations.

Test case:

["gin", "zen", "gig", "msg"]

Python:

class Solution(object):
  def uniqueMorseRepresentations(self, words):
  """
  :type words: List[str]
  :rtype: int
  """
  if not words:

    return 0
  wordDic = {
"a":".-",
"b":"-...",
"c":"-.-.",
"d":"-..",
"e":".",
"f":"..-.",
"g":"--.",
"h":"....",
"i":"..",
"j":".---",
"k":"-.-",
"l":".-..",
"m":"--",
"n":"-.",
"o":"---",
"p":".--.",
"q":"--.-",
"r":".-.",
"s":"...",
"t":"-",
"u":"..-",
"v":"...-",
"w":".--",
"x":"-..-",
"y":"-.--",
"z":"--.."

    }
  result = set()
  for w in words:
    word = ‘‘
    for x in w:
      word += wordDic.get(x)
    result.add(word)
  return len(result)

Analysis of Code:

It give me some words in a list, and I need to return a number about the word.

1. So the first I need to go through the list.

2. And then I also need to get all the letters in words, which go through the words.

3. Get all the letters corresponding to the Morse Code.

4. Put in a set.(set can carry out de-duplication )

原文地址:https://www.cnblogs.com/sxuer/p/10630492.html

时间: 2024-11-09 05:01:22

7. Unique Morse Code Words的相关文章

LeetCode 804. Unique Morse Code Words

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 &

Unique Morse Code Words

Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1)problem International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to

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

参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: 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 &q

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

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

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

morse code

morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代码,它的代码包括5种: 点.划.点和划之间的停顿.每个字符间短的停顿(在点和划之间).每个词之间中等的停顿以及句子之间长的停顿. 下图为morse code的密码本 通过密码本,我们可以写1个测试程序 懂了这些,morse code基本入门了. 接着,说一下电报机原理 首先,电报机是由发报机与收报机

CSUOJ 1269 Morse Code Wristwatch

Description Tokyoflash推出了一款摩斯电码手表,样图如下. 要读懂这款手表,首先要了解一些摩斯电码的知识. 在了解了这些摩斯电码符号之后,就不难看懂表盘中显示的信息是“02 17 PM”了,也就是指下午2点17分.在12小时计时法中小时的取值范围为01到12之间的整数,AM代表上午,PM代表下午. 接下来我们就详细介绍一下这款摩斯电码手表是如何显示时间的. 手表的表盘是一个5*18的点阵,由6个5*3的点阵自左至右排列而成,每个5*3的点阵都显示了一个数字或字母的信息. 对于

6kyu Decode the Morse code

题目: Part of Series 1/3 This kata is part of a series on the Morse code. After you solve this kata, you may move to the next one. 系列的一部分,1/3 这个形是莫尔斯电码系列的一部分.当你解决了这个问题后,你可能会转到下一个.In this kata you have to write a simple Morse code decoder. While the Mor