Accepted Java solution(8 ms)


  • 0
    E

    Simple logic to iterate through two pointers by considering alphanumeric values only for comparison.
    public class Solution {
    public boolean isPalindrome(String s) {
    char ch[]=s.toLowerCase().toCharArray();
    for(int i=0,j=ch.length-1;i<j;)
    {

            int hval=ch[i];
            int tval=ch[j];
            while(i<j && !((hval>=48 && hval<=57) ^ (hval>=97 && hval<=122)))
            hval=ch[++i];
             while(i<j && !((tval>=48 && tval<=57) ^ (tval>=97 && tval<=122)))
            tval=ch[--j];
            if(i>j)
            break;
            if(hval!=tval)
            return false;
            i++;
            j--;
        }
        return true;
        
        
        
    }
    

    }


Log in to reply
 

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