My 5ms easy Java solution beats 97.26% may help u


  • 2
    B
    public class Solution {
        public boolean isPalindrome(String s) {
            if ("".equals(s))
    			return true;
    		for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
    			char ci = s.charAt(i);
    			while (i < j
    					&& !(ci >= 'a' && ci <= 'z' || ci >= 'A' && ci <= 'Z' || ci >= '0' && ci <= '9'))
    				ci = s.charAt(++i);
    
    			char cj = s.charAt(j);
    			while (i < j
    					&& !(cj >= 'a' && cj <= 'z' || cj >= 'A' && cj <= 'Z' || cj >= '0' && cj <= '9'))
    				cj = s.charAt(--j);
    
    			if (i < j) {
    				if (ci >= 'A' && ci <= 'Z')
    					ci += 32;
    				if (cj >= 'A' && cj <= 'Z')
    					cj += 32;
    				if (ci != cj)
    					return false;
    			}
    		}
    		return true;
        }
    }

Log in to reply
 

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