-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppsolution1.cpp
42 lines (28 loc) · 1.17 KB
/
cppsolution1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
//All possible codes
string totCodes[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
//Vector holding all string codes.
vector<string> codes;
//Vector holding temp string of code and its iterator
string tempString;
vector<string>::iterator iter;
//Get words in array words.
for(int i = 0; i<words.size(); i++){
tempString = "";
//Get chars in word.
for(int j = 0; j<words.at(i).length(); j++){
//cout << totCodes[words.at(i).at(j) - 'a'];
tempString.append(totCodes[(words.at(i).at(j) - 'a')]);
}//end for
iter = find(codes.begin(), codes.end(), tempString);
//Fill codes.
if(iter == codes.end()){
iter = codes.begin();
iter = codes.insert(iter, tempString);
}
}//end for
return codes.size();
}//end function
};//end class