Golang solution


  • 0
    func longestPalindrome(s string) string {
        if len(s) < 2 {
            return s
        }
        max := 0
        start := 0
        for i:= 0; i<len(s); i++ {
            lo, so := extendPalindrome(s,i,i)
            if max < lo {
                max = lo
                start = so
            }
            le, se := extendPalindrome(s,i,i+1)
            if max < le {
                max = le
                start = se
            }
        }
        return s[start:start+max]
    }
    func extendPalindrome(s string, i, j int) (length, start int) {
        for ; i>=0 && j<len(s) && s[i] == s[j]; {
            i -= 1
            j += 1
        }
        return j-i-1, i+1
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.