Solution with O(n) time complexity using 2 pointers techniq

amazon
Tags: #<Tag:0x00007f2428b834b0>

#1

public class Solution {
public int solve(String a) {

    int j = a.length() - 1;
    int i = 0;
    int flg = 0;
    
    while(i<j){
        
        if(a.charAt(i) != a.charAt(j)) {
            if(flg == 0){
                if(a.charAt(i) == a.charAt(j-1)) {
                    j--;
                } else if (a.charAt(i+1) == a.charAt(j)) {
                    i++;
                }
                flg = 1;
                continue;
            } else{
                break;
            }
        }
        i++;j--;
    }
    if(i<j)
        return 0;
    return 1;
}

}


#2

Bro can please provide the intuition, I’m unable to understand the approach.


#3

Your solution will fail for abaab
I can delete first a to get a palindrome
Your approach checks only one side for deletion
But deletion can be from both sides when there is a mismatch
I don’t know how it got accepted