forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1268.Search-Suggestions-System_v1.cpp
74 lines (66 loc) · 1.82 KB
/
1268.Search-Suggestions-System_v1.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
class TrieNode
{
public:
TrieNode* next[26];
bool isEnd;
TrieNode()
{
for (int i=0; i<26; i++)
next[i]=NULL;
isEnd=0;
}
};
class Solution {
TrieNode* root;
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord)
{
root = new TrieNode();
for (auto& word: products)
{
TrieNode* node = root;
for (char ch: word)
{
if (node->next[ch-'a']==NULL)
node->next[ch-'a'] = new TrieNode();
node = node->next[ch-'a'];
}
node->isEnd = true;
}
vector<vector<string>>rets;
TrieNode* node=root;
string word;
for (int i=0; i<searchWord.size(); i++)
{
char ch = searchWord[i];
if (node->next[ch-'a']==NULL)
{
for (int j=i; j<searchWord.size(); j++)
rets.push_back({});
break;
}
node = node->next[ch-'a'];
word.push_back(ch);
vector<string>ans;
string str = "";
DFS(node,ans,str);
for (int j=0; j<ans.size(); j++)
ans[j] = word+ans[j];
rets.push_back(ans);
}
return rets;
}
void DFS(TrieNode* node, vector<string>&ans, string& str)
{
if (ans.size()==3) return;
if (node->isEnd == true)
ans.push_back(str);
for (int i=0; i<26; i++)
{
if (node->next[i]==NULL) continue;
str.push_back('a'+i);
DFS(node->next[i],ans, str);
str.pop_back();
}
}
};