C++ 5-line very concise two-pointer O(N) solution


  • 0

    Simply scan the string from both ends towards center, ignore non-alphanumeric chars and compare valid chars case insensitively.

        bool isPalindrome(string s) {
          auto L = s.begin(), R = --s.end();
          while (L < R)
            if (!isalnum(*L)) ++L; else if (!isalnum(*R)) --R;
            else if (toupper(*L++) != toupper(*R--)) return false;
          return true;
        }
    

Log in to reply
 

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