Accepted Java solution


  • 0
    A
    public class Solution {
        public boolean isPalindrome(String s) {
            if(s == "" )
                return true;
            
            int a = 0;
            int b = s.length()-1;
            
            while(a < b) {
                if(!Character.isLetterOrDigit(s.charAt(a))) {
                    a++;
                    continue;
                }
                if(!Character.isLetterOrDigit(s.charAt(b))) {
                    b--;
                    continue;
                }
                if(Character.toLowerCase(s.charAt(a)) == Character.toLowerCase(s.charAt(b))) {
                    a++;
                    b--;
                } else
                    return false;
            }
            return true;
        }
    }

  • 0
    H

    I am new to Java. Thanks for posting this. I learned the handy isLetterOrDigit method. =D
    A small suggestion:
    if(s == "" )
    return true;
    is not mandatory. When it's an empty string, it will not go to the while loop (0>-1) and go to return true at the end.

    Thanks a lot. =D


  • 0
    L

    Is "continue" inside your "if" necessary?


Log in to reply
 

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