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

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

原文地址:https://www.cnblogs.com/lightwindy/p/9847603.html

时间: 2024-07-31 21:28:52

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

[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

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 &

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

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

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

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

CSUOJ 1269 Morse Code Wristwatch

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

html隐写术,使用摩尔兹电码/莫尔兹电码存储信息 水波纹样式 Morse code

html水波纹样式,源码直接下载,代码有注释教程,小白可以看懂. 动画啥的都做好了,效果我觉得还不错 网上文章看到xbox 工程师使用隐写术,在界面的右下角放上了含有用户激活码的水波纹样式,一般人还真看不出来. 于是仿制了一个 先把莫尔兹转码部分放上来吧,莫尔兹转换部分是网上某位大佬写的,照抄. 完整代码下载在最后边 var utils = utils || {}; utils.isArray = function(value) { return Object.prototype.toStrin

[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