Java Program using two pointers


  • 0
    C
    public class Solution {
        public boolean isPalindrome(String s) {
            if(s ==  null || s.length() ==0) return true;
            int i =0, j = s.length()-1;
            while (i <= j) {
                char ch = s.charAt(i);
                char chJ = s.charAt(j);
                if(!isAlphanumeric(ch)) { i++; continue;}
                if(!isAlphanumeric(chJ)) {j--;continue;}
                if(Character.toLowerCase(ch) == Character.toLowerCase(chJ)) {
                    i++; j--;
                    continue;
                } else {
                            return false;
                        }
            }       
            return true;
        }
        
    public boolean isAlphanumeric(char ch) {
        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
            return true;
        } else {
            return false;
        }
    }
    

    }


Log in to reply
 

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