程序媛詹妮弗
终身学习

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'.

思路:

 

代码:

 1 class Solution {
 2     public boolean validPalindrome(String s) {
 3         int l = 0;
 4         int r = s.length()-1;
 5         while(l < r){
 6             if(s.charAt(l) != s.charAt(r)){
 7                 // try to delete a left side char || delete a right side char
 8                 return isPalin(s, l+1,r) || isPalin(s, l,r-1);
 9             }else{
10                 l++;
11                 r--;
12             }       
13         }
14         return true;
15         
16     }
17         
18    // helper function to judge valid palindrome      
19    private boolean isPalin(String s , int l, int r){   
20        while(l < r){
21            if(s.charAt(l++)!=s.charAt(r--)) return false;
22        }
23        return true;
24    }
25 }

 

posted on 2018-10-18 07:36  程序媛詹妮弗  阅读(97)  评论(0编辑  收藏  举报