Java 7 ms, beats 90%


  • 0
    A
    public boolean isPalindrome(String s) {
            int lo = 0;
            int hi = s.length() - 1;
            char loCh, hiCh;
            while (lo < hi) {
                loCh = s.charAt(lo);
                if (!Character.isLetterOrDigit(loCh)) {
                    lo++;
                    continue;
                }
                hiCh = s.charAt(hi);
                if (!Character.isLetterOrDigit(hiCh)) {
                    hi--;
                    continue;
                }
                if (Character.toLowerCase(loCh) != Character.toLowerCase(hiCh)) {
                    return false;
                }
                lo++;
                hi--;
            }
            return true;
        }
    

Log in to reply
 

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