forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_336.java
49 lines (44 loc) · 1.57 KB
/
_336.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _336 {
public static class Solution1 {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> pairs = new ArrayList();
Map<String, Integer> map = new HashMap();
for (int i = 0; i < words.length; i++) {
map.put(words[i], i);
}
for (int i = 0; i < words.length; i++) {
int l = 0;
int r = 0;
while (l <= r) {
String s = words[i].substring(l, r);
Integer j = map.get(new StringBuilder(s).reverse().toString());
if (j != null && j != i && isPalindrome(
words[i].substring(l == 0 ? r : 0, l == 0 ? words[i].length() : l))) {
pairs.add(
Arrays.asList(l == 0 ? new Integer[]{i, j} : new Integer[]{j, i}));
}
if (r < words[i].length()) {
r++;
} else {
l++;
}
}
}
return pairs;
}
private boolean isPalindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
}
}