Skip to content

Latest commit

 

History

History
40 lines (36 loc) · 982 Bytes

valid_palindrome2.md

File metadata and controls

40 lines (36 loc) · 982 Bytes

680. Valid Palindrome II

  • implementation type of problem.
  • have to made choice only once.
    • if left != right, then either skip left or right.
  • for readability, used recursion.
class Solution {
    public:
    string s;
    bool isPalindrome(int l, int r) {
        for (l, r; l <= r; l++, r--)
            if (s[l] != s[r])
                return false;
        return true;
    }

    bool validPalindrome(string s) {
        this->s = s;
        int left = 0;
        int right = s.size() - 1;
        int count = 0;

        while (left <= right) {
            if (s[left] == s[right]) {
                left ++;
                right --;
            }
            else {
                if (s[left] != s[right]) {
                    return (isPalindrome(left + 1, right) or
                            isPalindrome(left, right - 1));
                }
            }
        }
        return true;
    }
};