Java solution no regex. 260ms runtime... O(n) time, O(1) space... anything to improve?


  • 0
    B
    public class Solution {
        public boolean isPalindrome(String s) {
            for(int l = 0, r = s.length() - 1; l < r; l++, r--) {
                char lc;
                do {
                    lc = Character.toLowerCase(s.charAt(l));
                } while(!Character.isLetterOrDigit(lc) && l++ < r);
                
                char rc;
                do {
                    rc = Character.toLowerCase(s.charAt(r));
                } while(!Character.isLetterOrDigit(rc) && l < r--);
                
                if(lc != rc)
                    return false;
            }
            return true;
        }
    }

Log in to reply
 

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