804. Unique Morse Code Words
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
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 most100
. - Each
words[i]
will have length in range[1, 12]
. words[i]
will only consist of lowercase letters.
題目其實蠻簡單的,
其實想一下馬上就有解答了,
如果想不出來的朋友,
就往下拉吧 !
#include <iostream>
#include <string>
#include <vector>
#include<unordered_set>
using namespace std;
void PrintDifferenceMosCode(unordered_set<string>&MosCode)
{
for (unordered_set<string>::iterator str = MosCode.begin(); str != MosCode.end(); str++)
{
cout << *str << endl;
}
}
int uniqueMorseRepresentations(vector<string>& words)
{
vector<string> moscode = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
unordered_set<string>s;
for (auto word : words)
{
string temp;
for (auto c : word)
temp += moscode[c - 'a'];
s.insert(temp);
}
PrintDifferenceMosCode(s);
return s.size();
}
void main()
{
vector<string>strWords;
strWords.push_back("gin");
strWords.push_back("zen");
strWords.push_back("gig");
strWords.push_back("msg");
int n = uniqueMorseRepresentations(strWords);
cout << "There are " << n << " different transformations."<<endl;
}