| 22 | } |
| 23 | |
| 24 | func expandPalindrome(s string, i, j int) string { |
| 25 | if i < 0 || j >= len(s) { |
| 26 | return "" |
| 27 | } |
| 28 | |
| 29 | // Don't expand if they're not already equal |
| 30 | if s[i] != s[j] { |
| 31 | return "" |
| 32 | } |
| 33 | |
| 34 | // expand i and j until they're out-of-bounds |
| 35 | for i > -1 && j < len(s) { |
| 36 | // if expanding would make the string no longer |
| 37 | // a palindrome or go out-of-bounds, break so |
| 38 | // we can return the palindromic substring |
| 39 | if i-1 < 0 || j+1 >= len(s) || s[i-1] != s[j+1] { |
| 40 | break |
| 41 | } |
| 42 | |
| 43 | i-- |
| 44 | j++ |
| 45 | } |
| 46 | |
| 47 | // return the palindromic substring |
| 48 | return s[i : j+1] |
| 49 | } |
| 50 | |
| 51 | // Note: study again |
| 52 | func longestPalindrome0(s string) string { |