-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathLongestPalindromicSubstring.java
148 lines (103 loc) · 2.83 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Source: https://leetcode.com/problems/longest-palindromic-substring/
Approach 1 (Brute Force Approach, but will give TLE)
Time: O(n ^ 3), where n is the length of the given String(s)
Space: O(1), in-place
*/
class Solution {
public String longestPalindrome(String s) {
int maxLen = 0;
int left = 0;
int len = s.length();
for(int i = 0; i < len; ++i) {
for(int j = i; j < len; ++j) {
String sub = s.substring(i, j + 1);
if(isPalindrome(sub)) {
int subLen = sub.length();
if(subLen > maxLen) {
left = i;
maxLen = subLen;
}
}
}
}
return s.substring(left, left + maxLen);
}
private boolean isPalindrome(String sub) {
int start = 0;
int end = sub.length() - 1;
while(start < end && sub.charAt(start) == sub.charAt(end)) {
++start;
--end;
}
return start >= end;
}
}
/*
Approach 2 (Brute force too, but acceptable)
Time: O(n ^ 3), where n is the length of the given String(s)
Space: O(1), in-place
*/
class Solution {
public String longestPalindrome(String s) {
int maxLen = 0;
String longestSub = String.valueOf(s.charAt(0));
int len = s.length();
outer:
for(int i = 0; i < len; ++i) {
for(int j = len - 1; j > i; --j) {
while(j > i && s.charAt(i) != s.charAt(j)) {
--j;
}
int x = i;
int y = j;
while(x < y && s.charAt(x) == s.charAt(y)) {
++x;
--y;
}
// to check whether the substring is palindrome or not, where x == y is for odd length and x > y is for even length
if(x >= y) {
// if length of palindrome substring is more than previous palindrome string, then make this substring as max
if(j - i > maxLen) {
maxLen = j - i;
longestSub = s.substring(i, j + 1);
// no need to iterate unnecessary eg: "abba", longest can't be more than length 4(i.e. "abba" or "aabba")
if(longestSub.length() + i == len) {
break outer;
}
}
}
}
}
return longestSub;
}
}
/*
Approach 3
Time: O(n ^ 2), where n is the length of the given String(s)
Space: O(1), in-place
*/
class Solution {
private int start;
private int maxLen;
private int len;
public String longestPalindrome(String s) {
len = s.length();
for(int i = 0; i < len; ++i) {
extendPalindrome(s, i, i);
extendPalindrome(s, i, i + 1);
}
return s.substring(++start, start + maxLen);
}
private void extendPalindrome(String s, int left, int right) {
while(left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
--left;
++right;
}
int subLen = right - left - 1;
if(subLen > maxLen) {
maxLen = subLen;
start = left;
}
}
}