Simple java solution


  • 0
    H
    public boolean isPalindrome(String s) {
        if (s.equals("")) {return true;}
        s = s.toLowerCase();
        int p1 = 0, p2 = s.length() - 1;
        while (p1 < p2) {
            while (!Character.isLetterOrDigit(s.charAt(p1)) && p1 < p2) {
                p1++;
            }
            while (!Character.isLetterOrDigit(s.charAt(p2)) && p1 < p2) {
                p2--;
            }
            if (s.charAt(p1) == s.charAt(p2)) {
                p1++;
                p2--;
            } 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.