Elegant 0ms C++ solution


  • 0
    R
    bool isValid(string s) 
    {
        stack<char> mem;
        const char* chars = "()[]{}";
    
        for (char c : s) 
        {
            auto idx = find( chars, chars+6, c) - chars;
            
            if (idx % 2 == 0)
                mem.push(c);
            else 
            {
                if (mem.empty() || mem.top() != chars[idx-1]) 
                    return false;
                mem.pop();
            }
        }
        
        return mem.empty();
    }

Log in to reply
 

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