234ms Java solution, I think it's not concise enough, plz point out any improvement


  • -1
    S
    public class Solution {
        
        public boolean isPalindrome(String s) {
            int head = 0;
            int tail = s.length()-1;
            if(tail < 1) {
                return true;
            }
            while(tail > head) {
                while(!valid(s.charAt(tail)) && tail>head) {
                    tail--;
                }
                while(!valid(s.charAt(head)) && head<tail) {
                    head++;
                }
                if(!compare(s.charAt(head), s.charAt(tail))) {
                    return false;
                }
                head++;
                tail--;
            }
            return true;
        }
        
        public boolean compare(char a, char b) {
            if(a>=97 && a<=122 && (a-32)==b) {
                return true;
            } else if(b>=97 && b<=122 && a==(b-32)) {
                return true;
            } else if(a == b) {
                return true;
            }
            return false;
        }
        
        public boolean valid(char a) {
            if((a>=48 && a<=57) || (a>=65 && a<=90) || (a>=97 && a<=122)) {
                return true;
            }
            return false;
        }
    }

Log in to reply
 

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