12/13 ms Java


  • 0
    W

    Didn't realize the existence of isLetterOrDigit function, doing it the hard way

    public class Solution {
        public boolean isPalindrome(String s) {
            if(s.isEmpty()) return true;
            s = s.toLowerCase();
            String p = " .,':;?!#@-\\\"()`";
            
            int left = 0, right = s.length()-1;
            
            while(left < right){
                while(s.charAt(left) != s.charAt(right)){
                    if( p.contains(Character.toString( s.charAt(left) )) ){
                        ++left;
                    } 
                    else if( p.contains(Character.toString( s.charAt(right) )) ){
                        --right;
                    } else{
                        return false;
                    }
                }
                ++left; --right;
            }
            
            return true;
        }
    }
    

Log in to reply
 

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