Java solution using two pointers


  • 0
    M
    public boolean isPalindrome(String s) {
            char[] c = s.toCharArray();
            int start = 0;
            int end = s.length()-1;
            
            while(start<end) {
                
                if(!Character.isAlphabetic(c[start]) && !Character.isDigit(c[start])) {
                	start++;
                	continue;
                }
                
                if(!Character.isAlphabetic(c[end]) && !Character.isDigit(c[end])) {
                    end--;
                    continue;
                }
                if(Character.toLowerCase(c[start])!=Character.toLowerCase(c[end])) return false;
                
                start++;
                end--;
            }
            
            return true;
        }
    

Log in to reply
 

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