forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
66 lines (51 loc) · 1.45 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Source : https://leetcode.com/problems/unique-word-abbreviation/
/// Author : liuyubobobo
/// Time : 2018-08-13
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cassert>
#include <stdexcept>
using namespace std;
/// Brute Force
/// Iterate all words in dictionary to see isUnique
/// Time Complexity: init : O(n)
/// isUnique : O(n)
/// Space Complexity: O(n)
class ValidWordAbbr {
private:
unordered_set<string> words;
public:
ValidWordAbbr(vector<string> dictionary):
words(dictionary.begin(), dictionary.end()) {}
bool isUnique(string word) {
for(const string& e: words){
if(e == word)
continue;
int l = e.size();
if(l == word.size() && e[0] == word[0] && e[l - 1] == word[l - 1])
return false;
}
return true;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<string> dictionary;
dictionary.push_back("deer");
dictionary.push_back("door");
dictionary.push_back("cake");
dictionary.push_back("card");
dictionary.push_back("hello");
ValidWordAbbr vwa(dictionary);
print_bool(vwa.isUnique("dear"));
print_bool(vwa.isUnique("cart"));
print_bool(vwa.isUnique("cane"));
print_bool(vwa.isUnique("make"));
print_bool(vwa.isUnique("hello"));
return 0;
}