-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0005_LongestPalindromicSubstring.java
More file actions
48 lines (40 loc) · 1.48 KB
/
Copy path_0005_LongestPalindromicSubstring.java
File metadata and controls
48 lines (40 loc) · 1.48 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
//-----------------------------------------------------------------------------
// Runtime: 316ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
package bigegg.leetcode._0001_0050;
public final class _0005_LongestPalindromicSubstring {
public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) {
return s;
}
int n2 = s.length() * 2 + 1;
char[] s2 = new char[s.length() * 2 + 1];
for (int i = 0; i < s.length(); i++) {
s2[i * 2] = '#';
s2[i * 2 + 1] = s.charAt(i);
}
s2[n2 - 1] = '#';
int[] p = new int[n2];
int range_max = 0, center = 0, longestPalindromicCenter = 0;
for (int i = 1; i < n2 - 1; i++) {
if (range_max > i) {
p[i] = p[center * 2 - i] < range_max - i ? p[center * 2 - i] : range_max - i;
}
while (i - 1 - p[i] >= 0 && i + 1 + p[i] < n2 && s2[i - 1 - p[i]] == s2[i + 1 + p[i]]) {
p[i]++;
}
if (p[i] + i > range_max) {
center = i;
range_max = p[i] + i;
}
if (p[i] > p[longestPalindromicCenter]) {
longestPalindromicCenter = i;
}
}
int range = p[longestPalindromicCenter];
int startIndex = (longestPalindromicCenter - range) / 2;
return s.substring(startIndex, startIndex + range);
}
}