680 Valid Palindrome II

680 Valid Palindrome II



class Solution {
    public boolean validPalindrome(String s) {
      int i = 0;
      int j = s.length() - 1;
      
      while (i < j && s.charAt(i) == s.charAt(j)){    //// no nested while loop
          i++;
          j--;
      }
      // either i = j or meet the first mismatch chars 
      if(i >= j) {
        return true;
      }else{
        return check(s, i, j-1) || check(s, i+1, j);
      }
    }
    
    private boolean check(String s, int i, int j){
      while(i < j){
        char start = s.charAt(i);
        char end = s.charAt(j);
        if(start != end) return false;
        i++;
        j--;
      }
      return true;
    }
  
}

 

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

posted on 2018-08-09 17:35  猪猪&#128055;  阅读(109)  评论(0)    收藏  举报

导航