Sunday, September 17, 2017

680. Valid Palindrome II

https://leetcode.com/problems/valid-palindrome-ii/description/
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
    bool valid(string& s, int i, int j, int del) {
        if(del == 2) return false;
        if(i>=j) return true;
        if(s[i]!=s[j])
            return valid(s, i+1, j, del+1) || valid(s, i, j-1, del+1);
        else return valid(s, i+1, j-1, del);
    }
    bool validPalindrome(string s) {
        return valid(s, 0, s.size()-1, 0);
    }

No comments:

Post a Comment