-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-336-Palindrome-Pairs.java
166 lines (140 loc) · 5.75 KB
/
LeetCode-336-Palindrome-Pairs.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
/*
Some Test Cases:
["abcd","dcba","lls","s","sssll"]
[[0,1],[1,0],[2,4],[3,2]]
["a",""]
[[0,1],[1,0]]
["aa","a"]
[[0,1],[1,0]]
*/
class Solution {
// 1. Trie
/*
https://leetcode.com/problems/palindrome-pairs/discuss/79195/O(n-*-k2)-java-solution-with-Trie-structure
Time: O(N*L^2), N is total number of words, L is average length of words
*/
// private class Node {
// char c;
// HashMap<Character, Node> children = new HashMap<>();
// Integer idx = null;
// List<Integer> palindromeIdx = new ArrayList<>();
// public Node(char c) {
// this.c = c;
// }
// }
// private class Trie {
// Node root;
// public Trie() {
// this.root = new Node('@');
// }
// public void insert(String[] words, int i) {
// Node curr = root;
// for (int k = words[i].length() - 1; k >= 0; k--) {
// if (isPalindrome(words[i], 0, k)) {
// curr.palindromeIdx.add(i);
// }
// char c = words[i].charAt(k);
// if (curr.children.containsKey(c)) {
// curr = curr.children.get(c);
// } else {
// Node newNode = new Node(c);
// curr.children.put(c, newNode);
// curr = newNode;
// }
// }
// curr.idx = i;
// }
// public void search(String[] words, int i, List<List<Integer>> res) {
// String s = words[i];
// Node curr = root;
// // Search part 1: words[i] length > trie path length.
// for (int j = 0; j < s.length(); j++) {
// // We reached one end in the trie path, check if the trie path matches prefix in the the words[i], and if the suffix of words[i] is palindrome
// if (curr.idx != null && curr.idx != i && isPalindrome(s, j, s.length() - 1)) {
// res.add(Arrays.asList(i, curr.idx));
// }
// if (!curr.children.containsKey(s.charAt(j))) {
// return;
// }
// curr = curr.children.get(s.charAt(j));
// }
// // Search part 2: words[i] length == trie path length
// if (curr.idx != null && curr.idx != i) {
// res.add(Arrays.asList(i, curr.idx));
// }
// // words[i] length <= trie path length
// for (int idx : curr.palindromeIdx) {
// if (i == idx) continue;
// res.add(Arrays.asList(i, idx));
// }
// }
// public Node getRoot() {
// return root;
// }
// }
// public List<List<Integer>> palindromePairs(String[] words) {
// Trie trie = new Trie();
// for (int i = 0; i < words.length; i++) {
// trie.insert(words, i);
// }
// List<List<Integer>> res = new ArrayList<>();
// for (int i = 0; i < words.length; i++) {
// trie.search(words, i, res);
// }
// return res;
// }
// private boolean isPalindrome(String str, int i, int j) {
// while (i < j) {
// if (str.charAt(i) != str.charAt(j)) return false;
// i++;
// j--;
// }
// return true;
// }
// 2. HashMap
/*
https://leetcode.com/problems/palindrome-pairs/discuss/79217/Accepted-short-Java-solution-using-HashMap
First, put all Strings and their indices in Map. <String, Integer>
For each String W in the array, reverse it (call reversed String "RVS"). Then use 2 pointers l and r to iterate all possible substrings of RVS. Note that either l==0 or r ==RVS.length() because otherwise the substring will definitely fail to form a palindrome with W.
example, "sssll" and "lls" can pair
reverse "sssll" => "llsss"
l == 0, r==0
r++ to iterate all substrings:"l","ll","lls".
We found a substring "lls" in the Map. So we need to make sure the rest of the String ---- "ss" must be palindrome by it self
Luckily "ss" is a palindrome, so we add index of "lls"and "sssll" in to the result.
*/
public List<List<Integer>> palindromePairs(String[] words) {
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < words.length; i++) map.put(words[i], i);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
String s = new StringBuilder(words[i]).reverse().toString();
int l = 0, r = 0, len = s.length();
while (l <= r) {
Integer j = map.get(s.substring(l, r));
if (j != null && i != j) {
if (l == 0 && isPalindrome(s, r, len - 1)) {
res.add(Arrays.asList(j, i)); // here we put shorter string in front of longer string
} else if (r == len && isPalindrome(s, 0, l - 1)) {
// Here we have to put into "else if", as ["abcd","dcba"] could add to result twich when we don't have "else" before if
res.add(Arrays.asList(i, j));
}
}
if (r < len) {
r++;
} else {
l++;
}
}
}
return res;
}
private boolean isPalindrome(String str, int i, int j) {
while (i < j) {
if (str.charAt(i) != str.charAt(j)) return false;
i++;
j--;
}
return true;
}
}