Valid Palindrome II


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'.


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


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)
    return true

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