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 "-.-.", 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:
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.

题目描述:我们可以将单词描述为摩斯密码,问在一组单词中有多少个代表的莫斯密码是不同的。

题目分析:LeetCode 929. Unique Email Addresses 很类似,也是为了过滤掉重复的元素,我们可以用set集合去过滤。然后把单词转换为莫斯密码,然后过滤即可。

python 代码:

class Solution(object):
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        maps = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        words_length = len(words)
        morse_code = set()
        for i in range(words_length):
            word_string = words[i]
            word_string_length = len(word_string)
            string_code = ''
            for i in range(word_string_length):
                string_code += maps[ord(word_string[i]) - 97]
            morse_code.add(string_code)

        return len(morse_code)

C++ 代码:

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        set<string> morse_code;
        vector<string> string_code(words.size());
        vector<string> maps = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
//  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
//  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        for(int i = 0; i < words.size(); i++){
            for(int j = 0; j < words[i].size(); j++){
                string_code[i].append(maps[words[i][j] - 'a']);
            }
        }
        for(int i = 0; i < string_code.size(); i++){
            morse_code.insert(string_code[i]);
        }
        return morse_code.size();
    }
};

原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/10158761.html

时间: 2024-10-08 19:16:40

LeetCode 804. Unique Morse Code Words的相关文章

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] 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

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] 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

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 s

[C++]LeetCode: 78 Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the m

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (ma

morse code

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