-
Notifications
You must be signed in to change notification settings - Fork 2
/
PalindromePairs.java
152 lines (123 loc) · 3.24 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
package com.algorithm.playground.leetcode.problems.lc300.lc330.lc336;
import java.util.*;
/**
* https://leetcode.com/problems/palindrome-pairs/description/
*/
@SuppressWarnings("Duplicates")
public class PalindromePairs {
class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
Set<List<Integer>> result = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
int min = Integer.MAX_VALUE;
for (int i = 0; i < words.length; i++) {
map.put(words[i], i);
min = Math.min(min, words[i].length());
}
for (int i = 0; i < words.length; i++) {
if (words[i].isEmpty()) {
for (String key : map.keySet()) {
int idx = map.get(key);
if (idx != i) {
if (isPalindrome(key)) {
result.add(Arrays.asList(idx, i));
}
}
}
} else {
if (map.containsKey("") && (isPalindrome(words[i]))) {
result.add(Arrays.asList(map.get(""), i));
}
StringBuilder w = new StringBuilder(words[i]).reverse();
for (int j = min; j <= w.length(); j++) {
if (isPalindrome(w.substring(j))) {
Integer idx = map.get(w.substring(0, j));
if (idx != null && idx != i) {
result.add(Arrays.asList(idx, i));
}
}
if (isPalindrome(w.substring(0, j - min))) {
Integer idx = map.get(w.substring(j - min));
if (idx != null && idx != i) {
result.add(Arrays.asList(i, idx));
}
}
}
}
}
return new ArrayList<>(result);
}
private boolean isPalindrome(String w) {
int middle = w.length() / 2;
int i = w.length() % 2 == 0 ? middle - 1 : middle;
int j = middle;
while (i <= j && i >= 0 && j < w.length()) {
if (w.charAt(i--) != w.charAt(j++)) {
return false;
}
}
return true;
}
}
class ExampleSolution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> result = new ArrayList<>();
Node root = new Node();
for (int i = 0; i < words.length; i++) {
add(root, words[i], i);
}
for (int i = 0; i < words.length; i++) {
search(words[i], i, root, result);
}
return result;
}
private void search(String w, int i, Node root, List<List<Integer>> list) {
for (int j = 0; j < w.length(); j++) {
if (root.idx >= 0 && root.idx != i && isPalindrome(w, j, w.length() - 1)) {
list.add(Arrays.asList(i, root.idx));
}
root = root.children[w.charAt(j) - 'a'];
if (root == null) {
return;
}
}
for (int j : root.list) {
if (i != j) {
list.add(Arrays.asList(i, j));
}
}
}
private void add(Node root, String w, int idx) {
for (int i = w.length() - 1; i >= 0; i--) {
int j = w.charAt(i) - 'a';
if (root.children[j] == null) {
root.children[j] = new Node();
}
if (isPalindrome(w, 0, i)) {
root.list.add(idx);
}
root = root.children[j];
}
root.list.add(idx);
root.idx = idx;
}
private boolean isPalindrome(String w, int i, int j) {
while (i < j) {
if (w.charAt(i++) != w.charAt(j--)) {
return false;
}
}
return true;
}
private class Node {
Node[] children;
int idx;
List<Integer> list;
private Node() {
children = new Node[26];
idx = -1;
list = new ArrayList<>();
}
}
}
}