题目链接: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();
}
};