I'm solving problem336 in Leetcode, while I found the highest upvoted answer here used Trie structure in java. So, I wrote it in C++ again. I tried to optimize, but the Runtime and Memory are still very bad. Initially, I used shared_ptr, then the runtime: ~800 ms, memory: 400Mb. Then, I used new pointer, it became ~300ms, 200Mb. Still much slower than Java (20ms, 50Mb). I tried several times, but it's still 10x slower. My code structure and logic is just the same with the java implemented code, but it's so slow. I cannot figure out why! here is my code:
struct TrieNode {
TrieNode *next[26];
int index = -1;
vector<int> list;
TrieNode(){
memset(next,NULL,sizeof(next));
}
~TrieNode() {
for (int i = 0; i < 26; ++i) {
if (next[i]) delete(next[i]);
}
}
};
bool isPalindrome(const string &word, int start, int end) {
while (start < end) {
if (word[start] != word[end])
return false;
++start;
--end;
}
return true;
}
int addword(TrieNode* root, const string &word, int index) {
for (int i = word.size() - 1; i >= 0; --i) {
int j = word[i] - 'a';
if (!(root->next[j])) {
root->next[j] = new TrieNode;
}
if (isPalindrome(word, 0, i))
root->list.push_back(index);
root = root->next[j];
}
root->index = index;
root->list.push_back(index);
return 0;
}
void search(TrieNode* root, vector<string> &words, int index, vector<vector<int>> &res) {
for (int i = 0; i < words[index].size(); ++i) {
if (root->index >= 0 && root->index != index && isPalindrome(words[index], i, words[index].size() - 1)) {
res.push_back({index, root->index});
}
root = root->next[words[index][i] - 'a'];
if (!root) return;
}
for (int i : root->list) {
if (i == index) continue;
res.push_back({index, i});
}
}
vector<vector<int>> palindromePairs(vector<string>& words) {
vector<vector<int>> res;
TrieNode *root = new TrieNode;
for (int i = 0; i < words.size(); ++i) {
addword(root, words[i], i);
}
for (int i = 0; i < words.size(); ++i) {
search(root, words, i, res);
}
delete root;
return res;
}