share my java solution!


  • 0
    T
    class Solution {
        public boolean validPalindrome(String s) {
            if(s==null){
                return false;
            }
            int i=0;
            int j=s.length()-1;
            while(i<j){
                if(s.charAt(i)!=s.charAt(j)){
                    if(palindrome(s,i,j-1) || palindrome(s,i+1,j)){
                        return true;
                    }else{
                        return false;
                    }
                }
                i++;
                j--;
            }
            
            return true;
        }
        
        public boolean palindrome(String s,int start,int end){
            if(start>=end){
                return true;
            }
            
            while(start<end){
                if(s.charAt(start)!=s.charAt(end)){
                      return false;    
                }
                start++;
                end--;
            }
            
            return true;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.