Skip to content

Commit e7ecaad

Browse files
authored
Merge pull request neetcode-gh#599 from KC10201/5-Longest-Palindromic-Substring
5-longest palindromic substring: Add Alternative Solutions
2 parents 646a2f3 + 92aaa69 commit e7ecaad

File tree

1 file changed

+92
-16
lines changed

1 file changed

+92
-16
lines changed
Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
class Solution {
1+
// Solution: Expanding Around Center
2+
3+
// Time Complexity: O(n^2)
4+
// Extra Space Complexity: O(1)
5+
6+
class Solution1 {
27

38
public String longestPalindrome(String s) {
49
int strLength = s.length();
@@ -12,29 +17,100 @@ public String longestPalindrome(String s) {
1217

1318
for (int i = 0; i < s.length(); i++) {
1419
//Odd length
15-
int l = i, r = i;
16-
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
17-
if ((r - l + 1) > resultLength) {
18-
result = s.substring(l, r + 1);
19-
resultLength = r - l + 1;
20+
int left = i, right = i;
21+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
22+
if ((right - left + 1) > resultLength) {
23+
result = s.substring(left, right + 1);
24+
resultLength = right - left + 1;
2025
}
21-
l -= 1;
22-
r += 1;
26+
left -= 1;
27+
right += 1;
2328
}
2429

2530
// even length
26-
l = i;
27-
r = i + 1;
28-
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
29-
if ((r - l + 1) > resultLength) {
30-
result = s.substring(l, r + 1);
31-
resultLength = r - l + 1;
31+
left = i;
32+
right = i + 1;
33+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
34+
if ((right - left + 1) > resultLength) {
35+
result = s.substring(left, right + 1);
36+
resultLength = right - left + 1;
3237
}
33-
l -= 1;
34-
r += 1;
38+
left -= 1;
39+
right += 1;
3540
}
3641
}
3742

3843
return result;
3944
}
4045
}
46+
47+
// Solution: A more Optimized Expand Around Center
48+
49+
// TIme Complexity: O(n^2)
50+
// Extra Space Complexity: O(1)
51+
52+
class Solution2 {
53+
public String longestPalindrome(String s) {
54+
int best = 0;
55+
int start = 0, end = 0;
56+
57+
for (int i = 0; i < s.length(); i++){
58+
59+
int left = i - 1;
60+
while (i < s.length() - 1 && s.charAt(i) == s.charAt(i + 1)) {
61+
++i;
62+
}
63+
64+
int right = i + 1;
65+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
66+
--left; ++right;
67+
}
68+
69+
if (right - left > best) {
70+
best = right - left;
71+
start = left+1;
72+
end = right;
73+
}
74+
75+
}
76+
77+
return s.substring(start, end);
78+
}
79+
}
80+
81+
// Solution: Dynamic Programming
82+
83+
// Time Complexity: O(n^2)
84+
// Extra Spce Complexity: O(n^2)
85+
86+
class Solution3 {
87+
public String longestPalindrome(String s) {
88+
int len = s.length();
89+
int left = 0, right = 1, max = 0;
90+
91+
var isPalindrome = new boolean[len][len];
92+
93+
for (int i = len-1; i >= 0; i--) {
94+
for (int j = i; j < len; j++) {
95+
if (i == j) {
96+
isPalindrome[i][j] = true;
97+
} else if (s.charAt(i) == s.charAt(j)) {
98+
if (j - i == 1) {
99+
isPalindrome[i][j] = true;
100+
} else {
101+
isPalindrome[i][j] = isPalindrome[i+1][j-1];
102+
}
103+
}
104+
105+
if (isPalindrome[i][j] && j-i+1 > max) {
106+
max = j-i+1;
107+
left = i;
108+
right = j+1;
109+
}
110+
}
111+
}
112+
113+
return s.substring(left, right);
114+
}
115+
}
116+

0 commit comments

Comments
 (0)