LC 680. Valid Palindrome II

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    bool validPalindrome(string s) {
        int first = 0, last = s.size()-1, cnt=0;
        bool res1 = true, res2 = true;
        while(first<last){
            if(s[first]!=s[last]){
                first++;
                cnt += 1;
                if(cnt<2){  
                    continue;
                }
                else{
                    res1 = false;
                    break;
                }
            }
            first++,last--;
        }
        first = 0,last = s.size()-1,cnt=0;
        while(first<last){
            if(s[first]!=s[last]){
                last--;
                cnt += 1;
                if(cnt<2){  
                    continue;
                }
                else{
                    res2 = false;
                    break;
                }
            }
            first++,last--;
        }
        if(res1||res2){
            return true;
        }
        return false;
    }
};


Dicussion Forum