Leetcode 680 Solution

This article provides solution to leetcode question 680 (valid-palindrome-ii)

https://leetcode.com/problems/valid-palindrome-ii

Solution

class Solution {
public:
    bool isPalindrome(string s) {
        int l = 0;
        int r = s.size() - 1;

        while (l < r)
            if (s[l++] != s[r--])
                return false;

        return true;
    }

    bool validPalindrome(string s) {
        int l = 0;
        int r = s.size() - 1;

        while (l < r)
        {
            if (s[l] == s[r])
            {
                l++;
                r--;
            }
            else
            {
                return isPalindrome(s.substr(l, r - l)) || isPalindrome(s.substr(l + 1, r - l));
            }
        }

        return true;
    }
};