python:
class Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
m = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
res = []
for i in words:
t = ''
for j in i:
t += m[ord(j) - ord('a')]
res.append(t)
ans = len(set(res))
return ans
c++:
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> alph = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
unordered_set<string> res;
for (auto word: words){
string t = "";
for (auto let: word)
t += alph[let - 'a'];
res.insert(t);
}
return res.size();
}
};