[LeetCode] 804. Unique Morse Code Words(map)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Selukwe/article/details/88194100

题目链接:804. Unique Morse Code Words

解题思路:没什么技巧,构造 map 作为密码字典,生成的摩斯密码插入到一个 unordered_set 中,因为元素具有唯一性,所以可以直接求个数。

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        std::map<char, string> morse{
            {'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', "--.."}};
        
        unordered_set<string> hash;
        
        for (string &s:words)
        {
            string t ("");
            for (char &c:s) t += morse[c];
            hash.insert(t);
        }
        return hash.size();
    }
};

猜你喜欢

转载自blog.csdn.net/Selukwe/article/details/88194100