-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueMorseCodeWords.cpp
More file actions
62 lines (53 loc) · 1.68 KB
/
Copy pathUniqueMorseCodeWords.cpp
File metadata and controls
62 lines (53 loc) · 1.68 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//task 804
//4ms 8.8mb
//using set structure - slower, but ordered input, all elements are unique
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words)
{
string table[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
string temp("");
set<string> morse;
for (auto el : words)
{
for (int i = 0; i < el.size(); i++)
{
temp += table[el[i] - 'a'];
}
morse.insert(temp);
temp = "";
}
return (words.size() > 1) ? morse.size() : words.size();
}
};
//using vector structure - faster, but unordered input, contain same elements
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words)
{
string table[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
string temp("");
int result(1);
vector<string> morse;
for (auto el : words)
{
for (int i = 0; i < el.size(); i++)
{
temp += table[el[i] - 'a'];
}
morse.push_back(temp);
temp = "";
}
for (int i = 1; i < morse.size(); i++)
{
int j = 0;
while (j < i)
{
if (morse[i] != morse[j]) j++;
else break;
}
if (i==j) result ++;
}
return (words.size() > 1) ? result : words.size();
}
};