C++ solution


  • 0
    T

    '''
    class Solution {
    public:
    std::unordered_map<char, int> open_braces {{'(', 0 }, {'{', 1}, {'[', 2}};
    std::unordered_map<char, int> closed_braces {{')', 0 }, {'}', 1}, {']', 2}};

    bool isValid(string s) {
        std::vector<char> braces;
        for(auto c: s) {
            if(open_braces.find(c) != open_braces.end())
                braces.push_back(c);
            else if (closed_braces.find(c) != closed_braces.end()) {
                if(braces.empty() || (closed_braces.find(c)->second != open_braces.find(braces.back())->second))
                    return false;
                braces.pop_back();
            }        
        }
        if(!braces.empty())
            return false;
        return true;
    }
    

    };
    '''


Log in to reply
 

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