skip to content
developertype

snippets

filter:96 snippets
cpp-lc-validparens·cpp·leetcode
#include <stack>
#include <string>
#include <unordered_map>

bool isValid(const std::string& s) {
    std::stack<char> st;
    std::unordered_map<char, char> match{{')', '('}, {']', '['}, {'}', '{'}};
    for (char c : s) {
        if (match.count(c)) {
            if (st.empty() || st.top() != match[c]) return false;
            st.pop();
        } else {
            st.push(c);
        }
    }
    return st.empty();
}