/
PalindromePairs.java
190 lines (157 loc) · 6.43 KB
/
PalindromePairs.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> res = new ArrayList<>();
PalindromeManager manager = new PalindromeManager(words);
TrieNode head = new TrieNode(), t = head;
int emptyIndex = -1;
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (words[i].isEmpty()) {
emptyIndex = i;
continue;
}
for (int j = word.length() - 1; j >= 0; j--) {
char c = word.charAt(j);
if (t.children[c - 'a'] == null) {
t.children[c - 'a'] = new TrieNode();
}
t = t.children[c - 'a'];
t.words.add(word);
}
t.index = i;
t = head;
}
if (emptyIndex != -1) {
for (int index: manager.getPalindromeIndices()) {
if (emptyIndex != index) {
res.add(List.of(emptyIndex, index));
res.add(List.of(index, emptyIndex));
}
}
}
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (word.isEmpty()) {
continue;
}
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
t = t.children[c - 'a'];
if (t != null) {
// If current trie position indicates a word terminal
if (t.index != -1 && t.index != i) {
// Right now we added upto i chars from word we are checking
// And here we have encountered another terminal word
// So concatenating chars encountered so far and the word will be a palindrome
// It will also be a palindrome if the remaining chars form a palindrome.
PalindromicPart part = manager.get(word);
if (part.isSuffixPalindrome(j + 1)) {
res.add(List.of(i, t.index));
}
}
} else {
break;
}
}
if (t != null) {
for (String check: t.words) {
PalindromicPart part = manager.get(check);
int checkIndex = manager.indexOf(check);
if (i != checkIndex && part.isPrefixPalindrome(check.length() - word.length() - 1)) {
res.add(List.of(i, checkIndex));
}
}
}
t = head;
}
return res;
}
class PalindromicPart {
// Set of indices such that s[0..index] is palindrome
Set<Integer> prefixPalindromeIndices = new HashSet<>();
// Set of indices such that s[index..s.length() - 1] is palindrome
Set<Integer> suffixPalindromeIndices = new HashSet<>();
boolean isPrefixPalindrome(int index) {
return prefixPalindromeIndices.contains(index);
}
boolean isSuffixPalindrome(int index) {
return suffixPalindromeIndices.contains(index);
}
private void processPrefixes(String s) {
StringBuilder sb = new StringBuilder();
StringBuilder rev = new StringBuilder();
char last = 0;
boolean prefixBroken = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
sb.append(c);
rev.insert(0, c);
if (last == 0) {
prefixPalindromeIndices.add(i);
last = c;
continue;
} else if (!prefixBroken && last == c) {
prefixPalindromeIndices.add(i);
continue;
} else {
prefixBroken = true;
}
if (c == rev.charAt(rev.length() - 1) && sb.toString().equals(new StringBuilder(sb).reverse().toString())) {
prefixPalindromeIndices.add(i);
}
last = c;
}
// Empty prefix is still a palindrome
prefixPalindromeIndices.add(s.length());
}
private void processSuffixes(String s) {
StringBuilder sb = new StringBuilder(s);
StringBuilder rev = new StringBuilder(s).reverse();
for (int i = 0; i < s.length(); i++) {
if (sb.toString().equals(rev.toString())) {
suffixPalindromeIndices.add(i);
}
sb.deleteCharAt(0);
rev.deleteCharAt(rev.length() - 1);
}
// Empty suffix is still a palindrome
suffixPalindromeIndices.add(s.length());
}
PalindromicPart(String s) {
processPrefixes(s);
processSuffixes(s);
}
}
class PalindromeManager {
Map<String, PalindromicPart> map = new HashMap<>();
Set<Integer> palindromeIndices = new HashSet<>();
Map<String, Integer> indexMap = new HashMap<>();
PalindromeManager(String[] words) {
for (int i = 0; i < words.length; i++) {
String word = words[i];
PalindromicPart p = new PalindromicPart(word);
map.put(word, p);
indexMap.put(word, i);
if (p.isSuffixPalindrome(0)) {
palindromeIndices.add(i);
}
}
}
int indexOf(String word) {
return indexMap.getOrDefault(word, -1);
}
PalindromicPart get(String word) {
return map.get(word);
}
Set<Integer> getPalindromeIndices() {
return palindromeIndices;
}
}
class TrieNode {
TrieNode[] children = new TrieNode[26];
// Index of the word that ends here
int index = -1;
// List of words formed by this trie
List<String> words = new ArrayList<>();
}
}