-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-5-Longest-Palindromic-Substring.java
More file actions
231 lines (190 loc) · 6.8 KB
/
LeetCode-5-Longest-Palindromic-Substring.java
File metadata and controls
231 lines (190 loc) · 6.8 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
LeetCode: https://leetcode.com/problems/longest-palindromic-substring/
LintCode: http://www.lintcode.com/problem/longest-palindromic-substring/
JiuZhang: http://www.jiuzhang.com/solutions/longest-palindromic-substring/
ProgramCreek: http://www.programcreek.com/2013/12/leetcode-solution-of-longest-palindromic-substring-java/
1.Naive Approach: loops to get each substring
Time Complexity: O(n^3)
Time Limit Exceeded
2.Dynamic Programming
Time Complexity: O(n^2)
Time Limit Exceeded
3.From center to expand to get palindrome
Time Complexity: O(n^2)
Space Complexity: O(1)
*/
class Solution {
// 1. Native solution
// public String longestPalindrome(String s) {
// int max = Integer.MIN_VALUE;
// String result = "";
// for (int i = 0; i < s.length(); i++) {
// for (int j = i + 1; j <= s.length(); j++) {
// if (isPalindromic(s, i, j - 1)) {
// if (max < j - i) {
// max = j - i;
// result = s.substring(i, j);
// }
// }
// }
// }
// return result;
// }
// private boolean isPalindromic(String s, int lo, int hi) {
// while (lo < hi) {
// if (s.charAt(lo++) != s.charAt(hi--)) return false;
// }
// return true;
// }
// 2. Native solution
/*
Time Limit Exceeded
*/
// public String longestPalindrome(String s) {
// String result = "";
// int maxLen = 0;
// if(s == null || s.length() <= 1) return s;
// int len = s.length();
// // Here must be right "<=", as substring method in Java will excluse the right bound.
// for(int right = 0; right <= len; right++){
// for(int left = 0; left <= right; left++){
// String curr = s.substring(left, right);
// if(isPalindrome(curr) && curr.length() > maxLen){
// maxLen = curr.length();
// result = curr;
// }
// }
// }
// return result;
// }
// private boolean isPalindrome(String s){
// int start = 0, end = s.length() - 1;
// while(start < end){
// if(s.charAt(start) != s.charAt(end)) return false;
// start++;
// end--;
// }
// return true;
// }
// 3. Extend
// public String longestPalindrome(String s) {
// String result = "";
// int max = 0;
// if(s == null || s.length() <= 1) return s;
// // sub string in odd length
// for (int i = 0; i < s.length(); i++) {
// String temp = getLongestStr(s, i, i);
// if (temp.length() > max) {
// result = temp;
// max = temp.length();
// }
// }
// for (int i = 0; i < s.length() - 1; i++) {
// String temp = getLongestStr(s, i, i + 1);
// if (temp.length() > max) {
// result = temp;
// max = temp.length();
// }
// }
// return result;
// }
// private String getLongestStr(String s, int i, int j) {
// String res = "";
// while (i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)) {
// res = s.substring(i, j + 1);
// i--;
// j++;
// }
// return res;
// }
// extend
// private int lo, maxLen;
// public String longestPalindrome(String s) {
// int len = s.length();
// if (len < 2)
// return s;
// for (int i = 0; i < len-1; i++) {
// extendPalindrome(s, i, i); //assume odd length, try to extend Palindrome as possible
// extendPalindrome(s, i, i+1); //assume even length.
// }
// return s.substring(lo, lo + maxLen);
// }
// private void extendPalindrome(String s, int j, int k) {
// while (j >= 0 && k < s.length() && s.charAt(j) == s.charAt(k)) {
// j--;
// k++;
// }
// if (maxLen < k - j - 1) {
// lo = j + 1;
// maxLen = k - j - 1;
// }
// }
// DP (2D array)
/*
https://leetcode.com/problems/longest-palindromic-substring/discuss/2921/Share-my-Java-solution-using-dynamic-programming
subproblem:
dp[i][j] - whether char at i and char at j are equal, and s [i + 1, j - 1] is a palindrome
recurrence relation
assume dp[0, i - 1][0, len] is optimized result
dp[i][j] = true if char(i) == char(j) && dp[i + 1][j - 1] == true
init:
dp[0][0] = true // only one first char is a palindrome
ans:
longest (j - i + 1)
*/
// public String longestPalindrome(String s) {
// int len = s.length();
// if (len < 2) {
// return s;
// }
// // subproblem
// boolean[][] dp = new boolean[len][len];
// // init
// dp[0][0] = true;
// // recurrence relation
// int start = 0, maxLen = 0;
// for (int i = len - 1; i >= 0; i--) {
// for (int j = i; j < len; j++) {
// dp[i][j] = (s.charAt(i) == s.charAt(j)) && (j - i < 3 || dp[i + 1][j - 1]);
// if (dp[i][j] && maxLen <= j - i + 1) {
// start = i;
// maxLen = j - i + 1;
// }
// }
// }
// return s.substring(start, start + maxLen);
// }
// DP (1D array) (best DP solution)
/**
Optimization: substring() is costy. It's better to record start index and length, and do one substring finally.
"abcba"
j = 4
- - - - T
j = 3
- - - T F
j = 2
_ _ T F F
j = 1
_ T F T F
j = 0
T F F F T <- the last "True" is the one with maximum length
*/
public String longestPalindrome(String s) {
int len = s.length();
if(len < 2) return s;
boolean dp[] = new boolean[len];
int start = 0;
int maxLen = 0;
for(int i = len - 1; i >= 0; i--){
for(int j = len - 1; j >= i; j--){
// for (int j = i; j < len; j++) { // doesn't work, because the dp array records the [i + 1, j] palindomic information. If we scan j from i to len - 1, the information will be overwritten.
dp[j] = (s.charAt(i) == s.charAt(j)) && (j - i < 3 || dp[j - 1]);
if(dp[j] && maxLen <= j - i + 1){
start = i;
maxLen = j - i + 1;
}
}
}
return s.substring(start, start + maxLen);
}
}