Here's my accepted C++ solution, please help in cleaning it up more.

  • -3
      bool isPalindrome(string s) {
            int n = s.length();
            string t =s;
        for(int i=0;i<t.length();i++){
            if(t[i] == ' '){t.erase(i,1);i--;}

    // I put the above for loop to first erase all the space ONLY to check for the case where it could only be space
    // Is there a function which checks if all the characters are space, then I need no do it. Please let me know.

     if(t.length()==0){return true;}

    // Here, after removing space, I am checking of length is 0, is so, all was space. //

     int i=0;
     int j = t.length()-1;
     // Here I am simply checking from front and end and ignoring non - alpha numeric.
         if(   (t[i]==t[j]) || (  toupper(t[i]) == toupper(t[j])      )){i++;j--;}
         else { return false;}
     return true;

    // So please let me know how to check for space without removing the spaces and then checking.

Log in to reply

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