804. Unique Morse Code Words

804. Unique Morse Code Words


题目

Leetcode题目

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.

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.使用map来一一对应相应的摩斯密码
【n为words的size,m为words中最长的string的length】

  • 时间复杂度:O(n * m)
  • 空间复杂度:O(n)
// First Solution (Runtime:7 ms)
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        // store the International Morse Code
        map<char, string> table;
        set<string> translate;
        // initialize table
        initTable(table);
        int num = words.size();
        for (int i = 0; i < num; i++) {
            int len = words[i].length();
            string temp = "";
            for (int j = 0; j < len; j++) {
                temp += table[words[i][j]];
            }
            translate.insert(temp);
        }
        return translate.size();
    }

private:
    void initTable(map<char, string>& table) {
        table['a'] = ".-";
        table['b'] = "-...";
        table['c'] = "-.-.";
        table['d'] = "-..";
        table['e'] = ".";
        table['f'] = "..-.";
        table['g'] = "--.";
        table['h'] = "....";
        table['i'] = "..";
        table['j'] = ".---";
        table['k'] = "-.-";
        table['l'] = ".-..";
        table['m'] = "--";
        table['n'] = "-.";
        table['o'] = "---";
        table['p'] = ".--.";
        table['q'] = "--.-";
        table['r'] = ".-.";
        table['s'] = "...";
        table['t'] = "-";
        table['u'] = "..-";
        table['v'] = "...-";
        table['w'] = ".--";
        table['x'] = "-..-";
        table['y'] = "-.--";
        table['z'] = "--..";
    }
};

2.利用vector和下标一一对应
【n为words的size,m为words中最长的string的length】

  • 时间复杂度:O(n * m)
  • 空间复杂度:O(n)
// Second Solution (Runtime:6 ms)
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        unordered_set<string> translate; // or set<string> translate;
        // store the International Morse Code
        vector<string> table = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        int num = words.size();
        for (int i = 0; i < num; i++) {
            int len = words[i].length();
            string temp = "";
            for (int j = 0; j < len; j++) {
                temp += table[words[i][j] - 'a'];
            }
            if (translate.find(temp) == translate.end()) {
                translate.insert(temp);
                // if use set, just use 'translate.insert(temp);' directly
            }
        }
        return translate.size();
    }
};

虽然第二种方法的时间复杂度和空间复杂度上没有太大的变化,但是第二种方法运行较第一种方法快的原因是少了“initTable()”函数的调用,并且第二种方法使用的是unordered_set,这样可以减少插入set时候进行的排序所耽误的时间。

猜你喜欢

转载自blog.csdn.net/joker_yy/article/details/79844415