-
Notifications
You must be signed in to change notification settings - Fork 43
/
LongestPalindromicSubstring.java
127 lines (102 loc) · 3.59 KB
/
LongestPalindromicSubstring.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
package LeetCodeJava.String;
// https://leetcode.com/problems/longest-palindromic-substring/
import java.util.ArrayList;
public class LongestPalindromicSubstring {
// V0
// IDEA : SLIDING WINDOW : for - while
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/String/longest-palindromic-substring.py
public String longestPalindrome(String s) {
if (s == null || s.length() == 0) {
return s;
}
ArrayList<String> tmp = new ArrayList<String>();
String res = "";
/** NOTE !!! for - while (sliding window) */
for (int i = 0; i < s.length(); i++) {
// case 1 : length is odd
// int l = i - 1;
// int r = i + 1;
// NOTE !!! we set l = i, r = i when "length is odd"
int l = i;
int r = i;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
// NOTE !!! we get res first, then do pointers (l, r) update
if (r - l + 1 > res.length()) {
res = s.substring(l, r+1); // NOTE !!! substring with idx l, r+1
}
l -= 1;
r += 1;
}
// case 2 : length is even
// NOTE !!! we set l = i -1, r = i when "length is odd"
l = i - 1;
r = i;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
// NOTE !!! we get res first, then do pointers (l, r) update
if (r - l + 1 > res.length()) {
res = s.substring(l, r+1); // NOTE !!! substring with idx l, r+1
}
l -= 1;
r += 1;
}
}
return res;
}
// V1
// IDEA : BRUTE FORCE + isPalindrome check
// https://leetcode.com/problems/longest-palindromic-substring/solutions/4212564/beats-96-49-5-different-approaches-brute-force-eac-dp-ma-recursion/
public String longestPalindrome_2(String s) {
if (s.length() <= 1) {
return s;
}
int maxLen = 1;
String maxStr = s.substring(0, 1);
for (int i = 0; i < s.length(); i++) {
for (int j = i + maxLen; j <= s.length(); j++) {
if (j - i > maxLen && isPalindrome(s.substring(i, j))) {
maxLen = j - i;
maxStr = s.substring(i, j);
}
}
}
return maxStr;
}
private boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
// V2
// IDEA : SLIDING WINDOW
// https://leetcode.com/problems/longest-palindromic-substring/solutions/3401644/detailed-recursive-explaination-with-pictures-in-c-java-python-dp-two-pointers/
int maxLen = 0;
int lo = 0;
public String longestPalindrome_3(String s) {
char[] input = s.toCharArray();
if(s.length() < 2) {
return s;
}
for(int i = 0; i<input.length; i++) {
expandPalindrome(input, i, i);
expandPalindrome(input, i, i+1);
}
return s.substring(lo, lo+maxLen);
}
public void expandPalindrome(char[] s, int j, int k) {
while(j >= 0 && k < s.length && s[j] == s[k]) {
j--;
k++;
}
if(maxLen < k - j - 1) {
maxLen = k - j - 1;
lo = j+1;
}
}
}