3
bool isValid(string s) {4
stack<char> st; // taking stack for keep tracking the order of the6
for (auto i : s) // iterate over each and every elements8
if (i == '(' or i == '{' or i == '[')9
st.push(i); // if current element of the string will be opening bracket10
// then we will just simply push it into the stack11
else // if control comes to else part, it means that current element is a12
// closing bracket, so check two conditions current element matches13
// with top of the stack and the stack must not be empty...15
if (st.empty() or (st.top() == '(' and i != ')') or (st.top() == '{' and i != '}') or16
(st.top() == '[' and i != ']'))18
st.pop(); // if control reaches to that line, it means we have got the19
// right pair of brackets, so just pop it.22
return st.empty(); // at last, it may possible that we left something into the23
// stack unpair so return checking stack is empty or not..