-
Notifications
You must be signed in to change notification settings - Fork 481
/
0005.go
42 lines (39 loc) · 856 Bytes
/
0005.go
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
func longestPalindrome(s string) string {
if len(s) == 0 {
return ""
}
sn := make([]byte, len(s)*2 + 2)
sn[0], sn[1] = '$', '#'
l := 2
for _, val := range []byte(s) {
sn[l] = val
l++
sn[l] = '#'
l++
}
p := make([]int, l)
mid, r := 0, 0
mi, mr := 0, 0
for i := 1; i < l; i++ {
if r > mid {
if p[2*mid - i] < r - i {
p[i] = p[2*mid - i]
} else {
p[i] = r - i
}
} else {
p[i] = 1
}
for i + p[i] < l && sn[i - p[i]] == sn[i + p[i]] {
p[i]++
}
if i + p[i] > r {
r, mid = p[i] + i, i
}
if p[i] - 1 > mi {
mi = p[i] - 1
mr = (i - p[i]) / 2
}
}
return s[mr:mr+mi]
}