Leetcode--680. Valid Palindrome II(easy)

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


class Solution {
    public boolean validPalindrome(String s) {
        int l=-1,r=s.length();
        while(++l<--r){
            if(s.charAt(l)!=s.charAt(r))
                return isPalindrome(s,l+1,r)||isPalindrome(s,l,r-1);
        }
        return true;
    }
    
    public boolean isPalindrome(String s,int l,int r){
        while(l<r){
            if(s.charAt(l)!=s.charAt(r))
                return false;
            l++;
            r--;
        }
        return true;
    }
}

猜你喜欢

转载自www.cnblogs.com/albert67/p/10360997.html