-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.java
More file actions
27 lines (23 loc) · 779 Bytes
/
5.java
File metadata and controls
27 lines (23 loc) · 779 Bytes
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
class Solution {
public String longestPalindrome(String s) {
if (s == null || s.length() < 2) return s;
int start = 0, end = 0;
for (int index = 0; index < s.length(); index++) {
int len1 = expand(s, index, index);
int len2 = expand(s, index, index + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = index - (len - 1) / 2;
end = index + len / 2;
}
}
return s.substring(start, end + 1);
}
int expand(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return right - left - 1;
}
}