C++ O(n) Short & Elegant

  • 0

    The code explains itself to me.

    class Solution {
        bool ifPalindrome(string& s, int st, int ed, bool tolerant) {
            for(; st < ed; st++, ed--) {
                if (s[st] != s[ed]) 
                    if (tolerant)  return ifPalindrome(s, st+1, ed, false) || ifPalindrome(s, st, ed-1, false);
                    else return false;
            return true;
        bool validPalindrome(string s) {
            return ifPalindrome(s, 0, s.size()-1, true);

Log in to reply

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