-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.go
More file actions
48 lines (40 loc) · 805 Bytes
/
solution.go
File metadata and controls
48 lines (40 loc) · 805 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
41
42
43
44
45
46
47
48
package longest_palindromic_substring
func longestPalindrome(s string) string {
result := string(s[0])
for i := range s {
if i == 0 {
continue
}
substr := findFrontPalindrome(s, i)
if len(substr) > len(result) {
result = substr
}
substr = findSidePalindrome(s, i)
if len(substr) > len(result) {
result = substr
}
}
return result
}
func findFrontPalindrome(s string, pivot int) string {
start := pivot - 1
end := pivot
for {
if start < 0 || end > len(s)-1 || s[start] != s[end] {
return s[start+1 : end-1+1]
}
start -= 1
end += 1
}
}
func findSidePalindrome(s string, pivot int) string {
start := pivot - 1
end := pivot + 1
for {
if start < 0 || end > len(s)-1 || s[start] != s[end] {
return s[start+1 : end-1+1]
}
start -= 1
end += 1
}
}