Skip to content

Commit 45d75c3

Browse files
authored
Create unique-morse-code-words.cpp
1 parent 8f973e9 commit 45d75c3

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

C++/unique-morse-code-words.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n), n is the sume of all word lengths
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int uniqueMorseRepresentations(vector<string>& words) {
7+
static const vector<string> MORSE =
8+
{ ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
9+
"....", "..", ".---", "-.-", ".-..", "--", "-.",
10+
"---", ".--.", "--.-", ".-.", "...", "-", "..-",
11+
"...-", ".--", "-..-", "-.--", "--.."};
12+
13+
unordered_set<string> lookup;
14+
for (const auto& word : words) {
15+
string code;
16+
for (const auto& c : word) {
17+
code += MORSE[c - 'a'];
18+
}
19+
lookup.emplace(move(code));
20+
}
21+
return lookup.size();
22+
}
23+
};

0 commit comments

Comments
 (0)