Valid Palindrome II


#1

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:

Input: "aba"
Output: True

Example 2:

Input: "abca"
Output: True
Explanation: You could delete the character 'c'.

Note:

  1. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.

#2

Do a regular palindrome check first, whenever the characters doesn’t match, ignore that character and see if the rest of strings match.

func validPalindrome(s string) bool {
    l, r := 0, len(s)-1
    for l<=r {
        if s[l]!=s[r]{
            return isPalindrome(s, l, r-1) || isPalindrome(s, l+1, r)
        }
        l++
        r--
    }
    
    return true
}

func isPalindrome(s string, i, j int) bool{
    for i<=j{
        if s[i]!=s[j]{ return false}
        i++
        j--
    }
    return true
}