leetcode 804 Unique Morse Code Words
阿新 • • 發佈:2018-12-09
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(); } };