Problem Statement

Given a string s, return true if the s can be palindrome after deleting at most one character from it.

Example 1:

Input: s = "aba"
Output: true

Example 2:

Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.

Example 3:

Input: s = "abc"
Output: false

Constraints:

Problem Link

Valid Palindrome II - LeetCode

Code

class Solution {
public:
    bool validPalindrome(string s) {
        
        int n = s.size();
        
        int l = 0, r = n-1;
        
        while(l<=r)
        {
            if(s[l]==s[r])//if chars at both enda are equal
            {
                l++;
                r--;
            }
            
            else //if chars at both enda are not equal
            {
                int ll = l+1, lr = r;//Case 1: Remove 1 char from left
                
                while(ll<=lr&&s[ll]==s[lr])
                {
                    ll++;
                    lr--;
                }
                if(ll>lr)
                    return true;//Given string is pallindrome by removing char at index l
                
                int rl = l, rr = r-1;//Case 2: Remove 1 char from right
                
                while(rl<=rr&&s[rl]==s[rr])
                {
                    rl++;
                    rr--;
                }
                if(rl>rr)
                    return true;//Given string is pallindrome by removing char at index r
                
                return false;
            }
        }
        
        return true;//The given string was pallindrome
    }
    
    
  
};