Java code


  • 0
    J
        public boolean isPalindrome(String s) {
            if (s.isEmpty()) return true;
            int i = 0, j = s.length() - 1;
            while (i < j) {
                char c1 = s.charAt(i);
                char c2 = s.charAt(j);
                if (c1 >= 'a' && c1 <= 'z') c1 = (char)('A' + c1 - 'a');
                if (c2 >= 'a' && c2 <= 'z') c2 = (char)('A' + c2 - 'a');
                if (!(c1 >= 'A' && c1 <= 'Z' || c1 >= '0' && c1 <= '9')) {
                    i++;
                    continue;
                }
                if (!(c2 >= 'A' && c2 <= 'Z' || c2 >= '0' && c2 <= '9')) {
                    j--;
                    continue;
                }
                if (c1 != c2) return false;
                i++;
                j--;
            }
            return i >= j;
        }
    

Log in to reply
 

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