leetcode 680 https://leetcode.com/problems/valid-palindrome-ii/ asks to find a palindrome after deleting at most one character from it. I wrote the following code, but it turns out to fail for string "aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga" because the r value becomes 80 in the ends program entering the outer else case in which the functions return false. The program has passed the most tests so I'm not sure what's the problem of this program.
public boolean validPalindrome(String s) {
int l = 0, r = s.length() - 1;
boolean deleted = false;
while (l < r) {
System.out.println(s.charAt(l));
if (s.charAt(l) == s.charAt(r)) {
l++;
r--;
} else if (!deleted) {
deleted = true;
if (s.charAt(l + 1) == s.charAt(r)) {
l++;
} else if (s.charAt(l) == s.charAt(r - 1)) {
r--;
} else {
return false;
}
} else {
return false;
}
}
return true;
}