forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
50 lines (46 loc) · 1.26 KB
/
Solution.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
class Trie {
public:
vector<Trie*> children;
bool isEnd;
Trie(): children(26), isEnd(false) {}
};
class Solution {
public:
Trie* trie;
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
sort(words.begin(), words.end(), [&](const string & a, const string & b){
return a.size() < b.size();
});
vector<string> ans;
trie = new Trie();
for (auto& word : words)
{
if (word.size() == 0) continue;
if (dfs(word, 0)) ans.push_back(word);
else insert(word);
}
return ans;
}
bool dfs(string word, int u) {
Trie* node = trie;
if (u == word.size()) return true;
for (int i = u; i < word.size(); ++i)
{
int idx = word[i] - 'a';
node = node->children[idx];
if (!node) return false;
if (node->isEnd && dfs(word, i + 1)) return true;
}
return false;
}
void insert(string word) {
Trie* node = trie;
for (char c : word)
{
int idx = c - 'a';
if (!node->children[idx]) node->children[idx] = new Trie();
node = node->children[idx];
}
node->isEnd = true;
}
};