-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path005.go
51 lines (42 loc) · 1.01 KB
/
005.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
43
44
45
46
47
48
49
50
51
package p005
/**
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
*/
func longestPalindrome(s string) string {
sLen := len(s)
longCount := make([]int, sLen)
longestIndex := 0
findInnerLastChar := func(index int) int {
lastIndex := index
for s[lastIndex] == s[index] {
lastIndex++
if lastIndex >= sLen {
break
}
}
return lastIndex - 1
}
for i := 0; i < sLen; i++ {
lastIndex := findInnerLastChar(i)
var j int
for j = 1; i-j >= 0 && lastIndex+j < sLen; j++ {
if s[i-j] != s[lastIndex+j] {
break
}
}
longCount[i] = (lastIndex - i + 1) + 2*(j-1)
if longCount[i] > longCount[longestIndex] {
longestIndex = i
}
}
lastIndex := findInnerLastChar(longestIndex)
left := longestIndex - (longCount[longestIndex]-(lastIndex-longestIndex+1))/2
return string([]byte(s)[left : left+longCount[longestIndex]])
}