6ms Java solution


  • 0
    T
    public class Solution {
        public boolean isPalindrome(String s) {
            if(s == null || s.length() <= 1) return true;
            int i = 0, j = s.length() - 1, diff;
            while(j > i) {
                while (isNotAlphanumeric(s.charAt(i))) {
                    i++;
                    if(i == s.length()) break;
                }
                while (isNotAlphanumeric(s.charAt(j))) {
                    j--;
                    if(j == -1) break;
                }
                if(i == s.length() && j == -1) return true;
                diff = Math.abs(s.charAt(i) - s.charAt(j));
                if(diff == 0 || (diff == 32 && Math.max(s.charAt(i),s.charAt(j)) > 90)) {
                    i++;
                    j--;
                }
                else{
                    return false;
                }
            }
            return true;
        }
    
        private boolean isNotAlphanumeric(char s){
            return (s > 122 || s < 48 || (s > 90 && s < 97) || (s > 57 && s < 65));
        }
    }
    

Log in to reply
 

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