7ms Java solution with DIY helper function


  • 0
    P
    public class Solution {
        public boolean isPalindrome(String s) {
            int len = s.length();
            for (int i = 0, j = len-1; i < j; i++, j--) {
                while (i < len-1 && process(s.charAt(i)) == 0) {
                    i++;
                }
                while (j > 0 && process(s.charAt(j)) == 0) {
                    j--;
                }
                if (process(s.charAt(i)) != process(s.charAt(j))) {
                    return false;
                }
            }
            return true;
        }
        
        public int process(char ch) {
            if (ch >= 'a' && ch <= 'z') {
                return ch;
            }
            if (ch >= 'A' && ch <= 'Z') {
                return ch - ('A' - 'a');
            }
            if (ch >= '0' && ch <= '9') {
                return ch;
            }
            return 0;
        }
    }

Log in to reply
 

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