Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da6b87d

Browse files
committed
336_Palindrome_Pairs
1 parent 1c62aa1 commit da6b87d

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Remember solutions are only solutions to given problems. If you want full study
104104
| 298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/298_Binary_Tree_Longest_Consecutive_Sequence.py) | Bottom-up or top-down recursion, O(n) and O(n) |
105105
| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/305_Number_of_Islands_II.py) | Quick union find with weights, O(nlogn) and O(n) |
106106
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/322_Coin_Change.py) | Bottom-up or top-down DP, dp[n] = min(dp[n], dp[n - v_i]), where v_i is the coin, O(amount * n) and O(amount) |
107+
| 336 | [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/336_Palindrome_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/336_Palindrome_Pairs.java) | 1. Create a reverse word to index map, then for each word, check prefix and posfix, O(nk^2) and O(n)<br>2. Tire tree, O(nk^2) and O(n) |
107108
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/337_House_Robber_III.py) | 1. Recursion with hash map, O(n) and O(n)<br>2. Recursion on two steps, O(n) and O(1) |
108109
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
109110
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|

java/336_Palindrome_Pairs.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution {
2+
//https://leetcode.com/problems/palindrome-pairs/discuss/79195/O(n-*-k2)-java-solution-with-Trie-structure
3+
private static class TrieNode {
4+
TrieNode[] next;
5+
int index;
6+
List<Integer> list;
7+
8+
TrieNode() {
9+
next = new TrieNode[26];
10+
index = -1;
11+
list = new ArrayList<>();
12+
}
13+
}
14+
15+
public List<List<Integer>> palindromePairs(String[] words) {
16+
List<List<Integer>> res = new ArrayList<>();
17+
18+
TrieNode root = new TrieNode();
19+
20+
for (int i = 0; i < words.length; i++) {
21+
addWord(root, words[i], i);
22+
}
23+
24+
for (int i = 0; i < words.length; i++) {
25+
search(words, i, root, res);
26+
}
27+
28+
return res;
29+
}
30+
31+
private void addWord(TrieNode root, String word, int index) {
32+
for (int i = word.length() - 1; i >= 0; i--) {
33+
int j = word.charAt(i) - 'a';
34+
35+
if (root.next[j] == null) {
36+
root.next[j] = new TrieNode();
37+
}
38+
39+
if (isPalindrome(word, 0, i)) {
40+
root.list.add(index);
41+
}
42+
43+
root = root.next[j];
44+
}
45+
46+
root.list.add(index);
47+
root.index = index;
48+
}
49+
50+
private void search(String[] words, int i, TrieNode root, List<List<Integer>> res) {
51+
for (int j = 0; j < words[i].length(); j++) {
52+
if (root.index >= 0 && root.index != i && isPalindrome(words[i], j, words[i].length() - 1)) {
53+
res.add(Arrays.asList(i, root.index));
54+
}
55+
56+
root = root.next[words[i].charAt(j) - 'a'];
57+
if (root == null) return;
58+
}
59+
60+
for (int j : root.list) {
61+
if (i == j) continue;
62+
res.add(Arrays.asList(i, j));
63+
}
64+
}
65+
66+
private boolean isPalindrome(String word, int i, int j) {
67+
while (i < j) {
68+
if (word.charAt(i++) != word.charAt(j--)) return false;
69+
}
70+
71+
return true;
72+
}
73+
}

python/336_Palindrome_Pairs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def palindromePairs(self, words):
3+
"""
4+
:type words: List[str]
5+
:rtype: List[List[int]]
6+
"""
7+
# https://leetcode.com/problems/palindrome-pairs/discuss/79219/Python-solution~
8+
# reverse word and create a word to index map
9+
word2index, res = dict([(w[::-1], i) for i, w in enumerate(words)]), []
10+
for i, word in enumerate(words):
11+
for j in xrange(len(word) + 1):
12+
# Use prefix and postfix
13+
# rather than going through all posible combinations
14+
prefix, postfix = word[:j], word[j:]
15+
# prefix + postfix + reverse(prfix)
16+
if prefix in word2index and i != word2index[prefix] and postfix == postfix[::-1]:
17+
res.append([i, word2index[prefix]])
18+
# reverse(postfix) + prefix + postfix
19+
if j > 0 and postfix in word2index and i != word2index[postfix] and prefix == prefix[::-1]:
20+
res.append([word2index[postfix], i])
21+
return res

0 commit comments

Comments
 (0)