Clean and simple Java solution


  • 0
    J
    public class Solution {
        public boolean isPalindrome(String s) {
            if(s.length() == 0) return true;
            int head = 0;
            int tail = s.length() - 1;
            while(head < tail){
                //skip non alphanumeric characters
                while(head < s.length() && !Character.isLetterOrDigit(s.charAt(head))) head++;
                while(tail > 0 && !Character.isLetterOrDigit(s.charAt(tail))) tail--;
                if(head >= tail) return true;
                if(Character.toLowerCase(s.charAt(head)) == Character.toLowerCase(s.charAt(tail))){
                    head++;
                    tail--;
                }else return false;
            }
            return true;
        }
    }

Log in to reply
 

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