c++ solution


  • 0
    R
        bool isValid(string s) {
            stack<char> stk;
            unordered_map<char, char> openOfclose;
            
            openOfclose[')'] = '(';
            openOfclose['}'] = '{';
            openOfclose[']'] = '[';
            
            for(int i = 0; i < s.size(); i++) {
                if(s[i] == '(' || s[i] == '{' || s[i] == '[')
                    stk.push(s[i]);
                else {
                    if(stk.empty() || stk.top() != openOfclose[s[i]]) return false;
                    else stk.pop();
                }
            }
            
            return stk.empty();
        }
    

Log in to reply
 

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