0ms C++ easy solution


  • 0
    N
    #include <stack>
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> container;
            for(int i=0;i<s.length();i++){
                if(s[i] == '(' || s[i] == '{' || s[i] == '['){
                    container.push(s[i]);
                } else {
                    if( container.empty() || (s[i] == ')' && container.top() != '(') || (s[i] == '}' && container.top() != '{') || (s[i] == ']' && container.top() != '['))
                        return false;
                    container.pop();
                }
            }
            if(container.empty())
                return true;
            return false;
        }
    };
    

Log in to reply
 

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