https://leetcode.com/problems/valid-parentheses/description/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public:
bool isValid(string s) {
stack<char> st;
unordered_map<char, char> match = {{')', '('}, {'}', '{'}, {']', '['}};
for (char c : s) {
// closing bracket
if (match.count(c)) {
if (st.empty() || st.top() != match[c]) return false;
st.pop();
// opening bracket
} else {
st.push(c);
}
}
return st.empty();
}
};
Complexity analysis
((((((((((.