My simple, 9ms Accepted Solution in Java


  • 0
    P

    Used simple two pointer approach. Beats 69%.

    public class Solution {
        public boolean isPalindrome(String s) {
            int len = s.length();
            if (len == 0) return true;
            int i = 0, j = len-1;
            while(i <= j){
                char first = s.charAt(i);
                char last = s.charAt(j);
                if (Character.isLetterOrDigit(first) && Character.isLetterOrDigit(last)){
                    if (Character.toLowerCase(first) == Character.toLowerCase(last)){
                        i++; j--;
                    }
                    else return false;
                }
                else if (!Character.isLetterOrDigit(first)) i++;
                else j--;
            }
            return true;
        }
    }
    

Log in to reply
 

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