Valid Parentheses leetcode in c++
Valid Parentheses Leetcode Solution in C++
#include <stack>
#include <unordered_map>
class Solution {
public:
bool isValid(string s) {
stack<char> st;
unordered_map<char, char> mapping = {{')', '('}, {']', '['}, {'}', '{'}};
for (char c : s) {
if (mapping.count(c)) {
char topElement = st.empty() ? '#' : st.top();
if (topElement != mapping[c]) {
return false;
}
st.pop();
} else {
st.push(c);
}
}
return st.empty();
}
};