336. Palindrome Pairs
Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]Input: ["bat","tab","cat"]
Output: [[0,1],[1,0]]
Explanation: The palindromes are ["battab","tabbat"]class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>>res = new ArrayList<>();
TrieNode root = new TrieNode();
for(int i = 0; i < words.length; i++) addWords (root, words[i], i);
for(int i = 0; i < words.length; i++) search (root, words[i], i, res);
return res;
}
private void addWords(TrieNode root, String word, int index){
for(int i = word.length() - 1; i >=0; --i){
int pos = word.charAt(i) - 'a';
if (isPalindrome(word, 0, i))
root.cover.add(index);
// checking & traversing down to the trie tree
if(root.next[pos] == null) root.next[pos] = new TrieNode();
root = root.next[pos];
}
root.index = index;
root.cover.add(index);// since we have root node, in search when we go out of the for loop we
// ends up in the terminate node, so we need to add itslef to the cover list
}
private void search(TrieNode root, String word, int index, List<List<Integer>> res){
for(int i = 0; i < word.length(); i++){
if(root.index >= 0 && root.index != index && isPalindrome(word, i, word.length() - 1)) res.add(Arrays.asList(index, root.index));
int pos = word.charAt(i) - 'a';
if(root.next[pos] == null) return;
root = root.next[pos];
}
// add the cover list index
for(int i: root.cover){
if(i != index)
res.add(Arrays.asList(index, i));
}
}
private boolean isPalindrome(String word, int i, int j){
while(i < j){
if(word.charAt(i++) != word.charAt(j--)) return false;
}
return true;
}
}
class TrieNode{
int index;
TrieNode [] next;
List<Integer> cover;
TrieNode(){
index = -1;
next = new TrieNode [26];// consider small cases only
cover = new ArrayList<>();
}
}Last updated
Was this helpful?