Yet another O(n) time O(1) space solution in C++


  • -1
    bool validPalindrome(string s) {
        for (auto l = s.begin(), r = s.end() - 1; l < r; l++, r--)
            if (*l != *r)
                return judge(l + 1, r) || judge(l, r - 1);
        return true;
    }
    
    bool judge(string::iterator l, string::iterator r) {
        while (l < r)
            if (*l++ != *r--)
                return false;
        return true;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.