Unique Morse Code Words(C++唯一摩爾斯密碼詞)

解題思路:

(1)建立索引表,再將拼接成的字符串插入到集合中,最後返回集合的大小

class Solution {
public:
    string alpha[26]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    /**
     * @param words: the given list of words
     * @return: the number of different transformations among all words we have
     */
    int uniqueMorseRepresentations(vector<string> &words) {
        set<string> s;
        string str = "";
        for(auto&& w:words) {
            str = "";
            for(auto&& k:w) {
                str+=alpha[k-'a'];
            }
            s.insert(str);
        }
        return s.size();
    }
};

 

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