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

翻译:摩尔斯电码是使用.-来表示字母,如果直接将字符串转换成摩尔斯电码,而没有空格的话,那么不同的字符串可能有相同的摩尔斯电码,下面给出一系列字符串,给出这些字符串能够得到多少种不同类的摩尔斯电码。

思路:

1.首先根据字母拼接对应的摩尔斯电码,然后将这一段电码做hash映射,可以看做是一段01串,直接转化成二进制即可(可能溢出int范围,但是没有关系,相同的字符串溢出后也是一样的)

2.将一系列字符串对应的hash值排序,去重即可。

代码:

class Solution {
public:
    vector<string> mos={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    string to_mos(string &word)
    {
        string ans="";
        for(int i=0;i<word.size();i++)
            ans += mos[word[i]-‘a‘];
        return ans;
    }
    int removeDuplicates(vector<int>& nums)
    {
        int n=nums.size();
        if(n < 2) return n;
        int id = 1;
        for(int i = 1; i < n; i++)
            if(nums[i] != nums[i-1])
                nums[id++] =nums[i];
        return id;
    }
    int hash(string &m)
    {
        int ans=0;
        for(int i=0;i<m.size();i++)
        {
            if(m[i]==‘-‘) ans++;
            ans*=2;
        }
        return ans;
    }
    int uniqueMorseRepresentations(vector<string>& words)
    {
        int n=words.size();
        vector<string> mos_words(n);
        for(int i=0;i<n;i++)
            mos_words[i] = to_mos(words[i]);  

        vector<int> mos_words_hash(n,0);
        for(int i=0;i<n;i++)
            mos_words_hash[i] = hash(mos_words[i]);  

        sort(mos_words_hash.begin(),mos_words_hash.end());
        return removeDuplicates(mos_words_hash);
    }
};  

原文地址:https://www.cnblogs.com/zb-ml/p/8735314.html

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

Leetcode 804. 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 &

[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

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

摩尔斯电码(Morse Code)Csharp实现

摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到1999年.不过随着科技的发展,最终还是倒在了历史的车轮下(PS:等等,为何这么熟悉??观众:好一片根正苗红的*文啊,啊啊啊啊),1997年,当法国海军停止使用摩尔斯电码时,发送的最后一条消息是:“所有人注意,这是我们在永远沉寂之前最后的一声呐喊!”. 任何关于Morse code可拓展的咨询,请移步维基百科:摩尔

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

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