C++ Mutates the string, but ends up pretty clean


  • 0
    M

    This takes ~12 ms depending on the mood of the grader. Fast non-mutating solutions look to come in < 9 ms. Still, I'm happy with this code:

            bool isPalindrome(string s)
            {
                auto iter = remove_if(
                    begin(s), end(s),
                    [](char ch) { return !::isalnum(ch); });
    
                auto M = distance(begin(s), iter--) / 2;
    
                while (
                    M-- && 
                    (::tolower(s[M]) == ::tolower(iter[-M])));
    
                return M < 0;
            }
    

Log in to reply
 

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