Code gives WA for PRPALN, despite right logic

In the recently concluded NOV long contest, in the question PRPALN

I implemented a similar logic to the one in the editorial, but still was unable to solve it :

    int i, j, t, len, flag;
    string input;
	cin>>t;
	while(t--)
	{
		cin>>input;
		len=input.length();
		for(i=flag=0, j=len-1; i<=j; i++, j--)
		{
			if(input[i]==input[j]) continue;
			else if(input[i+1]==input[j] && flag!=1)
			{
				flag=1; i++;
			}
			else if(input[i]==input[j-1] && flag!=1)
			{
				flag=1; j--;
			}
			else
			{
				flag=2; break;
			}
		}
		if(flag!=2) cout<<"YES\n";
		else cout<<"NO\n";
	}
	return 0;

Here is the submission link : Submission
Please help.

1 Like

Your code fails for this case

hadadaha

On deleting last character, can form a palindrome, so answer is YES. but your code outputs NO

3 Likes

Thank you. I was searching for a test case that gave a wrong answer. Here is the AC code if anyone wishes to see : http://www.codechef.com/viewsolution/5440359