c++ solution easy to understand


  • 0
    Q
    class Solution {
    public:
        bool validPalindrome(string s) {
            int start = 0, end = s.size()-1;
            int cnt = 0;
            if(s.size() <= 1)
                return true;
            while(start < end){
                if(s[start] != s[end]){
                    cnt++;
                    return isPal(s,start+1,end) || isPal(s,start,end-1);
                }
                start++;
                end--;
            }
            return true;
        }
        
        bool isPal(string s, int i, int e){
            while(i<e){
                if(s[i] != s[e])
                    return false;
                i++;
                e--;
            }
            return true;
        }
    };
    

Log in to reply
 

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