private void search(String[] words, int i, PalTrieNode root, List <List <int> > res) { for (int j = 0; j < words[i].Length; j++) { if (root.index >= 0 && root.index != i && isPalindrome(words[i], j, words[i].Length - 1)) { res.Add(Arrays.AsList(i, root.index)); } root = root.next[words[i][j] - 'a']; if (root == null) { return; } } foreach (int j in root.list) { if (i == j) { continue; } res.Add(Arrays.AsList(i, j)); } }
public List <List <int> > palindromePairs(String[] words) { List <List <int> > res = new List <List <int> >(); PalTrieNode root = new PalTrieNode(); for (int i = 0; i < words.Length; i++) { addWord(root, words[i], i); } for (int i = 0; i < words.Length; i++) { search(words, i, root, res); } return(res); }
private void addWord(PalTrieNode root, String word, int index) { for (int i = word.Length - 1; i >= 0; i--) { int j = word[i] - 'a'; if (root.next[j] == null) { root.next[j] = new PalTrieNode(); } if (isPalindrome(word, 0, i)) { root.list.Add(index); } root = root.next[j]; } root.list.Add(index); root.index = index; }