forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromePairs.java
63 lines (56 loc) · 2.12 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
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;
}
}