1. 程式人生 > >Leetcode 804. Unique Morse Code Words 莫爾斯電碼重復問題

Leetcode 804. Unique Morse Code Words 莫爾斯電碼重復問題

odi post 題目 OS IE sdn unique 參考 tco

參考: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);  
    }  
};  

Leetcode 804. Unique Morse Code Words 莫爾斯電碼重復問題