Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:

Input: "aba"
Output: True

Example 2:

Input: "abca"
Output: True
Explanation: You could delete the character 'c'.

思路:

代码:

 class Solution {
public boolean validPalindrome(String s) {
int l = 0;
int r = s.length()-1;
while(l < r){
if(s.charAt(l) != s.charAt(r)){
// try to delete a left side char || delete a right side char
return isPalin(s, l+1,r) || isPalin(s, l,r-1);
}else{
l++;
r--;
}
}
return true; } // helper function to judge valid palindrome
private boolean isPalin(String s , int l, int r){
while(l < r){
if(s.charAt(l++)!=s.charAt(r--)) return false;
}
return true;
}
}
05-11 15:48