My java two-pointer solution


  • 0
    O
    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null || s.length() == 0) return true;
            for (int L = 0, R = s.length() - 1; L < R; L++, R--) {
                while (L < R && !Character.isLetterOrDigit(s.charAt(L))) L++;
                while (L < R && !Character.isLetterOrDigit(s.charAt(R))) R--;
                if (L < R && Character.toLowerCase(s.charAt(L)) != Character.toLowerCase(s.charAt(R))) {
                    return false;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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