Look at my answer, is easy to understand


  • -2
    W
    public static boolean isPalindrome(String string) {
        if(string == null) {
            return false;
        }
    
        StringBuffer stringBuffer = new StringBuffer();
        int length = string.length();
        for(int i=0; i<length; i++) {
            if(Character.isLetterOrDigit(string.charAt(i))) {
                stringBuffer.append(Character.toLowerCase(string.charAt(i)));
            }
        }
    
        string = stringBuffer.toString();
        length = string.length();
        if(length == 0 || length == 1) {
            return true;
        }
        for(int i=0; i<length/2; i++) {
            if(string.charAt(i) != string.charAt(length - i - 1)) {
                return false;
            }
        }
    
        return true;
    }

Log in to reply
 

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