My JAVA Solution


  • 0
    Q
        public boolean isPalindrome(String s) {
            if(s == null || s.length() == 0){
                return  true;
            }
            s = s.trim();
            s = s.toLowerCase();
    
            for(int i = 0, j = s.length() - 1; i <= j; i++, j--){
                while(i < s.length() && !Character.isAlphabetic(s.charAt(i)) && !Character.isDigit(s.charAt(i))){
                    i++;
                }
                while(j >= 0 && !Character.isAlphabetic(s.charAt(j)) && !Character.isDigit(s.charAt(j))){
                    j--;
                }
                if(i <= j && s.charAt(i) != s.charAt(j)){
                    return  false;
                }
            }
    
            return  true;
        }
    

Log in to reply
 

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