3
vector<TrieNode *> children;4
vector<int> wordIndexList;7
children = vector<TrieNode *>(26, NULL);13
// Root is a datamember of trie.19
root = new TrieNode();22
void insert(string &word, int wordIndex) {24
for (int i = 0; i < word.size(); i++) {25
int index = word[i] - 'a';26
if (!cur->children[index]) cur->children[index] = new TrieNode();27
cur = cur->children[index];28
cur->wordIndexList.push_back(wordIndex);32
vector<int> searchPrefOrSuff(string &str) {35
for (int i = 0; i < n; i++) {36
int index = str[i] - 'a';37
if (cur->children[index] == NULL) return {};38
cur = cur->children[index];40
return cur->wordIndexList;46
Trie *prefixTrie = new Trie();47
Trie *suffixTrie = new Trie();48
unordered_map<string, int> mp;50
WordFilter(vector<string> &words) {52
for (int i = 0; i < n; i++) {53
string word = words[i];54
prefixTrie->insert(word, i);57
reverse(rev.begin(), rev.end());58
suffixTrie->insert(rev, i);62
int f(string pref, string suff) {63
string key = pref + "-" + suff;64
if (mp.find(key) != mp.end()) return mp[key];66
vector<int> prefList = prefixTrie->searchPrefOrSuff(pref);67
reverse(suff.begin(), suff.end());68
vector<int> suffList = suffixTrie->searchPrefOrSuff(suff);70
int n = prefList.size(), m = suffList.size();71
int k = n - 1, l = m - 1;73
while (k >= 0 && l >= 0) {74
if (prefList[k] == suffList[l]) return mp[key] = prefList[k];75
if (prefList[k] > suffList[l])77
else if (suffList[l] > prefList[k])