-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathPalindromePairs336.java
188 lines (154 loc) · 5.61 KB
/
PalindromePairs336.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
/**
* Given a list of unique words, find all pairs of distinct indices (i, j) in
* the given list, so that the concatenation of the two words, i.e.
* words[i] + words[j] is a palindrome.
*
* Example 1:
* Given words = ["bat", "tab", "cat"]
* Return [[0, 1], [1, 0]]
* The palindromes are ["battab", "tabbat"]
*
* Example 2:
* Given words = ["abcd", "dcba", "lls", "s", "sssll"]
* Return [[0, 1], [1, 0], [3, 2], [2, 4]]
* The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
*
*/
public class PalindromePairs336 {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> res = new ArrayList<>();
for (int i=0; i<words.length; i++) {
for (int j=i+1; j<words.length; j++) {
String s1 = words[i];
String s2 = words[j];
if (isPalindrome(s1, s2)) {
List<Integer> pair = new ArrayList<>(2);
pair.add(i);
pair.add(j);
res.add(pair);
}
if (isPalindrome(s2, s1)) {
List<Integer> pair = new ArrayList<>(2);
pair.add(j);
pair.add(i);
res.add(pair);
}
}
}
return res;
}
private boolean isPalindrome(String s1, String s2) {
int i = 0;
int j = s2.length() - 1;
while (i < s1.length() && j >= 0) {
if (s1.charAt(i) != s2.charAt(j)) return false;
i++;
j--;
}
if (i < s1.length()) {
return isPalindrome(s1, i, s1.length()-1);
}
if (j >= 0) {
return isPalindrome(s2, 0, j);
}
return true;
}
private boolean isPalindrome(String str, int i, int j) {
if (str == null || str.length() <= 1) return true;
if ((j - i + 1) <= 0) return true;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) return false;
i++;
j--;
}
return true;
}
private boolean isPalindrome(String str) {
if (str == null || str.length() <= 1) return true;
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) return false;
i++;
j--;
}
return true;
}
/**
* https://leetcode.com/problems/palindrome-pairs/discuss/79199/150-ms-45-lines-JAVA-solution
*/
public List<List<Integer>> palindromePairs2(String[] words) {
List<List<Integer>> ret = new ArrayList<>();
if (words == null || words.length < 2) return ret;
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i=0; i<words.length; i++) map.put(words[i], i);
for (int i=0; i<words.length; i++) {
for (int j=0; j<=words[i].length(); j++) {
String str1 = words[i].substring(0, j);
String str2 = words[i].substring(j);
if (isPalindrome(str1)) {
String str2rvs = new StringBuilder(str2).reverse().toString();
if (map.containsKey(str2rvs) && map.get(str2rvs) != i) {
List<Integer> list = new ArrayList<Integer>();
list.add(map.get(str2rvs));
list.add(i);
ret.add(list);
}
}
if (isPalindrome(str2)) {
String str1rvs = new StringBuilder(str1).reverse().toString();
if (map.containsKey(str1rvs) && map.get(str1rvs) != i && str2.length()!=0) {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
list.add(map.get(str1rvs));
ret.add(list);
}
}
}
}
return ret;
}
/**
* https://leetcode.com/problems/palindrome-pairs/discuss/79195/O(n*k2)-java-solution-with-Trie-structure-(n:-total-number-of-words-k:-average-length-of-each-word)
*/
private static class TrieNode {
TrieNode[] next;
int index;
List<Integer> list;
TrieNode() {
next = new TrieNode[26];
index = -1;
list = new ArrayList<>();
}
}
public List<List<Integer>> palindromePairs3(String[] words) {
List<List<Integer>> res = new ArrayList<>();
TrieNode root = new TrieNode();
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(TrieNode root, String word, int index) {
for (int i = word.length() - 1; i >= 0; i--) {
int j = word.charAt(i) - 'a';
if (root.next[j] == null) root.next[j] = new TrieNode();
if (isPalindrome(word, 0, i)) root.list.add(index);
root = root.next[j];
}
root.list.add(index);
root.index = index;
}
private void search(String[] words, int i, TrieNode root, List<List<Integer>> 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].charAt(j) - 'a'];
if (root == null) return;
}
for (int j : root.list) {
if (i == j) continue;
res.add(Arrays.asList(i, j));
}
}
}