怕最後不夠寫 30天,連 set 也拿來用一下
Sets 集合,即一堆東西,該堆東西可能具有某種特定性質的事物,並將數個物件歸類而分成為一個或數個形態各異的大小整體
而在 C++ 中,set 主要操作有:
今天一樣以一題 leetcode 結束這一回合:
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, "cba" 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.
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> morse_code = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
unordered_set<string> gen_codes;
for(auto word : words) {
string code = "";
for(auto ch : word)
code += morse_code[ch - 'a'];
gen_codes.insert(code);
}
return gen_codes.size();
}