Easy C++ Solution


  • 1
    F
        bool isValid(string s) {
            list<char> stack;
            for (char c : s) {
                char expected = ' ';
                switch (c) {
                    case ')': expected = '('; break;
                    case '}': expected = '{'; break;
                    case ']': expected = '['; break;
                }
                if (expected != ' ') {
                    if (stack.empty() || stack.back() != expected)
                        return false;
                    stack.pop_back();
                } else {
                    stack.push_back(c);
                }
            }
            return stack.empty();
        }
    
    

  • 0
    H

    smart solution


Log in to reply
 

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