Two Pointer Java Solution, 94%, Easy Understand


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

Log in to reply
 

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