-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPalindromePairs.java
More file actions
143 lines (117 loc) · 4.62 KB
/
PalindromePairs.java
File metadata and controls
143 lines (117 loc) · 4.62 KB
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
package string;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Description: https://leetcode.com/problems/palindrome-pairs
* Difficulty: Hard
*/
public class PalindromePairs {
/**
* Time complexity: O(n^2 * length)
* Space complexity: O(n^2 + length)
*/
public List<List<Integer>> palindromePairsViaBruteForce(String[] words) {
List<List<Integer>> pairs = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (i != j && isPalindrome(words[i] + words[j])) {
pairs.add(List.of(i, j));
}
}
}
return pairs;
}
private boolean isPalindrome(String word) {
int left = 0;
int right = word.length() - 1;
while (left < right) {
if (word.charAt(left) != word.charAt(right)) return false;
left++;
right--;
}
return true;
}
/**
* Time complexity: O(n * length^2)
* Space complexity: O(n^2 + length^2)
*/
public List<List<Integer>> palindromePairsViaPrefixesAndSuffixes(String[] words) {
Map<String, Integer> wordToIndexMap = toMap(words);
List<List<Integer>> pairs = new ArrayList<>();
for (String word : words) {
List<Integer> reversedWordPairs = findReversedWordPairs(word, wordToIndexMap); // [abc] + [cba]
List<List<Integer>> reversedPrefixPairs = findReversedPrefixPairs(word, wordToIndexMap); // [abc]aba + [cba]
List<List<Integer>> reversedSuffixPairs = findReversedSuffixPairs(word, wordToIndexMap); // [cba] + aba[abc]
pairs.add(reversedWordPairs);
pairs.addAll(reversedPrefixPairs);
pairs.addAll(reversedSuffixPairs);
}
return pairs.stream().filter(l -> !l.isEmpty()).toList();
}
private List<Integer> findReversedWordPairs(String word, Map<String, Integer> wordToIndexMap) {
String reversed = new StringBuilder(word).reverse().toString();
int currentIndex = wordToIndexMap.get(word);
return wordToIndexMap.containsKey(reversed) && currentIndex != wordToIndexMap.get(reversed)
? List.of(currentIndex, wordToIndexMap.get(reversed))
: List.of();
}
private List<List<Integer>> findReversedSuffixPairs(String word, Map<String, Integer> wordToIndexMap) {
List<String> suffixes = getAllValidSuffixes(word);
List<List<Integer>> pairs = new ArrayList<>();
int currentIndex = wordToIndexMap.get(word);
for (String suffix : suffixes) {
String reversed = new StringBuilder(suffix).reverse().toString();
if (wordToIndexMap.containsKey(reversed)) {
pairs.add(List.of(wordToIndexMap.get(reversed), currentIndex));
}
}
return pairs;
}
private List<String> getAllValidSuffixes(String word) {
List<String> suffixes = new ArrayList<>();
for (int i = 0; i < word.length(); i++) {
if (isPalindrome(word, 0 , i)) {
suffixes.add(word.substring(i + 1));
}
}
return suffixes;
}
private List<List<Integer>> findReversedPrefixPairs(String word, Map<String, Integer> wordToIndexMap) {
List<String> prefixes = getAllValidPrefixes(word);
List<List<Integer>> pairs = new ArrayList<>();
int currentIndex = wordToIndexMap.get(word);
for (String prefix : prefixes) {
String reversed = new StringBuilder(prefix).reverse().toString();
if (wordToIndexMap.containsKey(reversed)) {
pairs.add(List.of(currentIndex, wordToIndexMap.get(reversed)));
}
}
return pairs;
}
private List<String> getAllValidPrefixes(String word) {
List<String> prefixes = new ArrayList<>();
for (int i = 0; i < word.length(); i++) {
if (isPalindrome(word, i, word.length() - 1)) {
prefixes.add(word.substring(0, i));
}
}
return prefixes;
}
private boolean isPalindrome(String word, int left, int right) {
while (left < right) {
if (word.charAt(left) != word.charAt(right)) return false;
left++;
right--;
}
return true;
}
private Map<String, Integer> toMap(String[] words) {
Map<String, Integer> wordToIndexMap = new HashMap<>();
for (int i = 0; i < words.length; i++) {
wordToIndexMap.put(words[i], i);
}
return wordToIndexMap;
}
}