1. 程式人生 > >leetcode 804 Unique Morse Code Words

leetcode 804 Unique Morse Code Words

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();
        
    }
};