expand at the center 2*n - 1 times for palindromes with even and odd lengths
(s string)
| 5 | // expand at the center 2*n - 1 times for |
| 6 | // palindromes with even and odd lengths |
| 7 | func longestPalindrome(s string) string { |
| 8 | longest := "" |
| 9 | for i := 0; i < len(s); i++ { |
| 10 | s1 := expandPalindrome(s, i, i) |
| 11 | s2 := expandPalindrome(s, i, i+1) |
| 12 | |
| 13 | if len(s1) > len(longest) { |
| 14 | longest = s1 |
| 15 | } |
| 16 | if len(s2) > len(longest) { |
| 17 | longest = s2 |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | return longest |
| 22 | } |
| 23 | |
| 24 | func expandPalindrome(s string, i, j int) string { |
| 25 | if i < 0 || j >= len(s) { |