5-liner with string iterators


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

Log in to reply
 

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