[LeetCode] Palindrome Pairs

336. Palindrome Pairs

Given a list of unique words, return all the pairs of the distinct indices (i, j) in the given list, so that the concatenation of the two words words[i] + words[j] is a palindrome.

  • Time : O(nk^2)
  • Space : O(branch)
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
struct Trie {
Trie* next[26];
vector<int> palindromes;
int eof;

Trie() {
memset(next,0,sizeof(next));
eof = -1;
}
};

class Solution {
Trie* rvTrie;
vector<vector<int>> res;
public:
//o(n) for iterating
//o(k^2) for palindrome
//we can caculate palindrome as k(lenth of word)
//k/2 + (k-1)/2 + (k-2)/2 ... 1/2 = (k) * (k + 1) / 2 / 2 = (k^2 + k)/4 = k^2
//so total time is o(nk^2)
vector<vector<int>> palindromePairs(vector<string>& words) {
rvTrie = new Trie();
for(int i = 0; i < words.size(); i++) {
insert(words[i], i);
}
for(int i = 0; i < words.size(); i++) {
find(words[i], i);
}

return res;
}
void insert(string& str, int& index) {
Trie* cur = rvTrie;
for(int p = str.length() - 1; p >= 0; p--) {
if(isPalindrome(str, 0, p)) {
cur->palindromes.push_back(index);
}
if(!cur->next[str[p]-'a']) cur->next[str[p]-'a'] = new Trie();
cur = cur->next[str[p]-'a'];

}
cur->palindromes.push_back(index);
cur->eof = index;
}

void find(string& str, int& idx) {
Trie* cur = rvTrie;
for(int i = 0; i < str.length(); i++) {
if(cur->eof != -1 and cur->eof != idx and isPalindrome(str,i,str.length() - 1)) {
res.push_back({idx,cur->eof});
}
if(!cur->next[str[i]-'a']) return ;
cur = cur->next[str[i]-'a'];
}
for(auto p : cur->palindromes) {
if(p == idx) continue;
res.push_back({idx, p});
}
}


bool isPalindrome(string& str, int l, int r) {
while(l < r) {
if(str[l++] != str[r--]) {
return false;
}
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/17/PS/LeetCode/palindrome-pairs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.