go lang simple solution


  • 0
    A
    func longestPalindrome(s string) string {
        if len(s) < 2 {
            return s
        }
        max := string(s[0])
        for i:=0;i<len(s);i++ {
            max = checkPalindrome(s, i, i, max)
            max = checkPalindrome(s, i, i+1, max)
        }
        return max
    }
    
    func checkPalindrome(s string, i int, j int, max string) string {
        leng := len(s)
        var sub string
        for i>=0 && j<=(leng - 1) && s[i] != s[j] {
            sub = s[i:j+1]
            i--
            j++
        }
        if len(max) < len(sub) {
            max = sub
        }
        return max
    }
    

Log in to reply
 

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