Simple java solution using Character class


  • 0
    C

    Go from both ends ignoring non-alphanumeric until pointers meet in the middle.

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s.length() == 0) return true;
            
            int start = 0;
            int end = s.length()-1;
            while (start < end) {
                while (start < s.length() && !Character.isLetterOrDigit(s.charAt(start))) start++;
                while (end > 0 && !Character.isLetterOrDigit(s.charAt(end))) end--;
                
                if (start > end) return true;
                if (Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(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.