Simple C++ solution


  • 0
    Y
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> history;
            
            history.push('0');
            
            for(auto item: s) {
                if(match(history.top(), item)) history.pop();
                else history.push(item);
            }
            
            return history.top() == '0';
        }
        
        bool match(char a, char b) {
            return (a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}');
        }
    };
    

Log in to reply
 

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