forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
38 lines (30 loc) · 992 Bytes
/
main.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
/// Source : https://leetcode.com/problems/unique-morse-code-words/description/
/// Author : liuyubobobo
/// Time : 2018-03-24
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
using namespace std;
/// Using Hash Set
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
unordered_set<string> bag;
for(const string& word: words){
string code = "";
for(char c: word)
code += morse[c - 'a'];
bag.insert(code);
}
return bag.size();
}
};
int main() {
vector<string> words = {"gin", "zen", "gig", "msg"};
cout << Solution().uniqueMorseRepresentations(words) << endl;
return 0;
}