My answer, welcome to review it.


  • 0
    P
    public class Solution {
        public boolean isPalindrome(String s) {
            if(s==null)
              return false;
            int nlen = s.length();
            if(nlen == 0 || nlen == 1)
              return true;
            int i=0,j=nlen-1;
            boolean ret = true;
            while(j>=i)
            {
                char pre = s.charAt(i);
                char post = s.charAt(j);
                if(checkLetter(pre) && checkLetter(post))
                {
                   if(letterEqual(pre, post))
                   {
                       i++;
                       j--;
                   }
                   else
                   {
                       ret = false;
                       break;
                   }
                }
                else{
                    if(!checkLetter(pre))
                    {
                        i++;
                    }
                    if(!checkLetter(post))
                    {
                        j--;
                    }
                }
            }
            return ret;
        }
        
        private boolean checkLetter(char input)
        {
            if( (input>='a' && input<='z') || (input>='A' && input<='Z') || (input>='0' && input<='9') )
               return true;
            else
               return false;
        }
        
        private boolean letterEqual(char left, char right)
        {
            int nLeft = (int)left;
            int nRight = (int)right;
            if((nLeft == nRight-32) || (nLeft==nRight+32) || (nLeft==nRight) )
              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.