-
Notifications
You must be signed in to change notification settings - Fork 0
/
211.cpp
100 lines (85 loc) · 1.99 KB
/
211.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<bits/stdc++.h>
using namespace std;
class WordDictionary {
private:
class trienode{
public:
vector<trienode*> characters;
bool isWordEnd;
trienode(): characters(26, nullptr), isWordEnd{false} {}
};
trienode* root;
void clear(trienode *root){
if(root){
for(trienode *no: root->characters)
clear(no);
delete root;
}
}
bool search(string word, trienode* root){
trienode *curr = root;
int n = word.size();
for(int i = 0; i < n; i++){
char c = word[i];
if(c != '.'){
trienode *next = curr->characters[c-'a'];
if(next == nullptr) return false;
curr = next;
}
else{
string remaining_word = string(word.begin()+i+1, word.end());
// U can also use std::any_of for the below lines.
for(auto no: curr->characters)
if(no != nullptr and search(remaining_word, no) == true)
return true;
return false;
}
}
return curr->isWordEnd;
}
public:
WordDictionary(): root{ new trienode{} } {}
~WordDictionary(){
clear(root);
}
void addWord(string word) {
trienode* curr = root;
for(char c: word){
trienode* next = curr->characters[c-'a'];
if(next == nullptr) next = curr->characters[c-'a'] = new trienode();
curr = next;
}
curr->isWordEnd = true;
}
bool search(string word) {
return search(word, this->root);
}
};
// TLE
class WordDictionary3 {
private:
unordered_set<string> dict;
public:
void addWord(string word) {
dict.insert(word);
}
bool search(string word) {
vector<string> possible_words { "" };
for(char c: word){
if(c != '.'){
for(auto &pw: possible_words) pw.push_back(c);
}
else{
vector<string> tmp;
for(auto &pw: possible_words)
for(char i = 'a'; i <= 'z'; i++)
tmp.push_back(pw+i);
possible_words = move(tmp);
}
}
return any_of(possible_words.begin(), possible_words.end(),
[dict=&dict](auto& word){ return dict->find(word) != dict->end(); });
}
};
int main(){
}