Cleanest Java Solution O(n) 4 lines, no regex


  • 0
    A

    '''
    public boolean isPalindrome(String s) {
    s = s.toLowerCase();
    int len = s.length()-1;
    for(int i = 0, j = len; i<=j; i++, j--){
    while(i<=j && !(Character.isDigit(s.charAt(i)) || Character.isLetter(s.charAt(i)))) i++;
    while(i<=j && !(Character.isDigit(s.charAt(j)) || Character.isLetter(s.charAt(j)))) j--;
    if(i<= j && s.charAt(i)!=s.charAt(j)) return false;
    }
    return true;

    }
    

    '''


Log in to reply
 

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