O(N), Easy Understand Solution.


  • 1
    Y
    public boolean validPalindrome(String s) {
            if(s == null || s.length() == 0) return true;
            int left = 0; int right = s.length() - 1;
            int count1 = 0;
            while(left < right){
                if(s.charAt(left) != s.charAt(right)){
                    count1++;
                    left ++;
                }else{
                    left ++;
                    right --;
                }
            }
            left = 0; right = s.length() - 1; int count2 = 0;
            while(left < right){
                if(s.charAt(left) != s.charAt(right)){
                    count2++;
                    right --;
                }else{
                    left ++;
                    right --;
                }
            }
            return count1 <= 1 || count2 <= 1; 
        }
    

Log in to reply
 

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