-
Notifications
You must be signed in to change notification settings - Fork 277
/
PalindromePairs.java
227 lines (190 loc) · 6.63 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class Solution {
// TC:O(n*k2)
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> res = new ArrayList();
Map<String, Integer> map = new HashMap();
for (int i = 0; i < words.length; ++i) {
map.put(words[i], i);
}
// Empty String case
if (map.containsKey("")) {
int blankIdx = map.get("");
for (int i = 0; i < words.length; ++i) {
if (i != blankIdx && isPalindrome(words[i])) {
res.add(Arrays.asList(blankIdx, i));
res.add(Arrays.asList(i, blankIdx));
}
}
}
// Reflection case
for (int i = 0; i < words.length; ++i) {
String reversed = new StringBuilder(words[i]).reverse().toString();
Integer reversedIdx = map.get(reversed);
if (reversedIdx != null && reversedIdx != i) {
res.add(Arrays.asList(i, reversedIdx));
}
}
// Tricky case
for (int i = 0; i < words.length; ++i) {
String cur = words[i];
for (int cut = 1; cut < cur.length(); ++cut) {
String left = cur.substring(0, cut);
String right = cur.substring(cut);
if (isPalindrome(left)) {
String reversedRight = new StringBuilder(right).reverse().toString();
if (map.containsKey(reversedRight)) {
res.add(Arrays.asList(map.get(reversedRight), i));
}
}
if (isPalindrome(right)) {
String reversedLeft = new StringBuilder(left).reverse().toString();
if (map.containsKey(reversedLeft)) {
res.add(Arrays.asList(i, map.get(reversedLeft)));
}
}
}
}
return res;
}
private boolean isPalindrome(String word) {
int i = 0, j = word.length() - 1;
while(i < j) {
if (word.charAt(i++) != word.charAt(j--)) return false;
}
return true;
}
}
//correction to original solution
class Solution {
List<List<Integer>> result = new ArrayList<>();
Map<String,Integer> map = new HashMap<>();
public List<List<Integer>> palindromePairs(String[] words) {
if(words == null || words.length == 0){
return result;
}
String[]reverse = new String[words.length];
for(int i=0;i<words.length;i++){
map.put(words[i],i);
reverse[i] = new StringBuilder(words[i]).reverse().toString();
}
if(map.containsKey("")){
for(int i=0;i<words.length;i++){
if(i != map.get("") && isPalindrome(words[i],0,words[i].length()-1)){
result.add(Arrays.asList(map.get(""),i));
result.add(Arrays.asList(i,map.get("")));
}
}
}
//reflection
for(int i=0;i<words.length;i++){
if(map.containsKey(reverse[i])){
Integer idx = map.get(reverse[i]);
if(idx != null && idx != i){
result.add(Arrays.asList(i,idx));
}
}
}
for(int i=0;i<words.length;i++){
String currentWord = words[i];
for(int cut=1;cut<currentWord.length();cut++){
String leftCut = currentWord.substring(0,cut);
String rightCut = currentWord.substring(cut);
if(isPalindrome(leftCut,0,leftCut.length()-1)){
String r = new StringBuilder(rightCut).reverse().toString();
if(map.containsKey(r)){
result.add(Arrays.asList(map.get(r),i));
}
}
if(isPalindrome(rightCut,0,rightCut.length()-1)){
String r = new StringBuilder(leftCut).reverse().toString();
if(map.containsKey(r)){
result.add(Arrays.asList(i,map.get(r)));
}
}
}
}
return result;
}
public boolean isPalindrome(String word,int start,int end){
while(start < end){
if(word.charAt(start++) != word.charAt(end--)){
return false;
}
}
return true;
}
}
//Trie based solution which works for new testcases too
class TrieNode{
TrieNode []children;
int id;
List<Integer>pos;
TrieNode(){
children=new TrieNode[26];
pos=new ArrayList<>();
id=-1;
}
}
class Solution {
TrieNode root;
List<List<Integer>>result;
Solution(){
root=new TrieNode();
result=new ArrayList<>();
}
public boolean isPalindrome(String word,int start,int end){
while(start<end){
if(word.charAt(start++)!=word.charAt(end--)){
return false;
}
}
return true;
}
public List<List<Integer>> palindromePairs(String[] words) {
for(int i=0;i<words.length;i++){
insertIntoTrie(words[i],i);
}
for(int i=0;i<words.length;i++){
searchInTrie(words[i],i);
}
return result;
}
public void insertIntoTrie(String word,int id){
TrieNode pCrawl=root;
for(int i=word.length()-1;i>=0;i--){
int index=word.charAt(i)-'a';
if(pCrawl.children[index]==null){
pCrawl.children[index]=new TrieNode();
}
if(isPalindrome(word,0,i)){
pCrawl.pos.add(id);
}
pCrawl=pCrawl.children[index];
}
pCrawl.id=id;
pCrawl.pos.add(id);
}
public void searchInTrie(String word,int id){
TrieNode pCrawl=root;
for(int i=0;i<word.length();i++){
int index=word.charAt(i)-'a';
if(pCrawl.id>=0&&pCrawl.id!=id&&isPalindrome(word,i,word.length()-1)){
List<Integer>res=new ArrayList<>();
res.add(id);
res.add(pCrawl.id);
result.add(res);
}
if(pCrawl.children[index]==null)
return;
pCrawl=pCrawl.children[index];
}
for(int i:pCrawl.pos){
if(i==id)
continue;
List<Integer>res=new ArrayList<>();
res.add(id);
res.add(i);
result.add(res);
}
}
}