-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.go
More file actions
40 lines (36 loc) · 720 Bytes
/
solution.go
File metadata and controls
40 lines (36 loc) · 720 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
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import "fmt"
func longestPalindrome(s string) string {
if len(s) == 0 {
return ""
}
start, end := 0, 0
for i := range s {
len1 := expandAroundCenter(s, i, i)
len2 := expandAroundCenter(s, i, i+1)
var length int
// check which one is longer
if len1 > len2 {
length = len1
} else {
length = len2
}
// find the start and end point
if length > end-start {
start = i - (length-1)/2
end = i + length/2
}
}
return s[start : end+1]
}
func expandAroundCenter(s string, left int, right int) int {
for left >= 0 && right < len(s) && s[left] == s[right] {
left--
right++
}
return right - left - 1
}
func main() {
s := "abbababad"
fmt.Println(longestPalindrome(s))
}