804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: “a” maps to “.-“, “b” maps to “-…”, “c” maps to “-.-.”, and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—”,”-.-“,”.-..”,”–”,”-.”,”—”,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–”,”-..-“,”-.–”,”–..”]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, “cab” can be written as “-.-.-….-“, (which is the concatenation “-.-.” + “-…” + “.-“). We’ll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Example:
Input: words = [“gin”, “zen”, “gig”, “msg”]
Output: 2
Explanation:
The transformation of each word is:
“gin” -> “–…-.”
“zen” -> “–…-.”
“gig” -> “–…–.”
“msg” -> “–…–.”

There are 2 different transformations, “–…-.” and “–…–.”.

Note:

The length of words will be at most 100.
Each words[i] will have length in range [1, 12].
words[i] will only consist of lowercase letters.

Java:

    public int uniqueMorseRepresentations(String[] words) {
        String[] morse = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};

        Set<String> set = new HashSet();
        for (String word : words) {
            StringBuilder sb = new StringBuilder();
            for (char ch : word.toCharArray()) {
                sb.append(morse[ch - 'a']);
            }
            set.add(sb.toString());
        }

        return set.size();
    }

C++:

class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> temp;
        for (int i = 0; i < words.size(); i++) {
            words[i] = decode(words[i]);
            bool found = false;
            for (int j = 0; j < temp.size(); j++) {
                if (words[i] == temp[j]) {
                   found = true;
                }    
            }
            if (!found) {
               temp.push_back(words[i]);            
            }
        }
        return temp.size();
    }
    string decode(string a) {
           string morse[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
           string temp = "";
           for (int i = 0; i < a.size(); i++) {
               temp += morse[a[i] - 97];
           }
           return temp;       
    }
};

class Solution1 {
public:
       int uniqueMorseRepresentations(vector<string>& words) {
           string morse[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
           set<string> myset;

           for (int i = 0; i < words.size(); i++) {
               string str = "";
               for (int j = 0; j < words[i].size(); j++) {
                   str += morse[words[i][j] - 'a'];    
               }    
               myset.insert(str);
           } 

           return myset.size();    
       }

};

Python:

class Solution(object):
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--",
                 "-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        s = set()
        for word in words:
            tmp = ''
            for ch in word:
                tmp += morse[ord(ch) - 97]
            s.add(tmp)

        return len(s)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章