C++ O(n) Short & Elegant


  • 0
    J

    The code explains itself to me.

    class Solution {
    public:
        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.