cpp solution, easy to understand.


  • 0
    class Solution {
    public:
      bool isPalindrome(string s) {
        if (s.size() == 0)
        {
          return true;
        }
    
        int i = 0, j = s.size() - 1;
        while (i <= j)
        {
          if (!isLetterOrDigit(s[i]))
          {
    	i ++;
          }
          else if (!isLetterOrDigit(s[j]))
          {
    	j --;
          }
          else
          {
    	if (tolower(s[i]) != tolower(s[j]))
            {
    	  return false;
            }
          
            i ++;
            j --;
          }
        }
        return true;
      }
      bool isLetterOrDigit(char ch)
      {
        if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        {
          return true;
        }
        return false;
      }
    };
    

Log in to reply
 

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