June 28, 2022 · 1 min read
https://practice.geeksforgeeks.org/problems/parenthesis-checker/0
class Solution {
public:
bool ispar(string x) {
if (x.size() % 2 != 0)
return false;
stack<char> st;
for (int i = 0; i < x.size(); i++) {
if (x[i] == '}') {
if (st.top() == '{')
st.pop();
}
if (x[i] == ']') {
if (st.top() == '[')
st.pop();
}
if (x[i] == ')') {
if (st.top() == '(')
st.pop();
} else {
st.push(x[i]);
}
}
return (st.empty() == true);
}
};