1
class Solution {
2
public:
3
bool isValid(string s) {
4
stack<char> st; // taking stack for keep tracking the order of the
5
// brackets..
6
for (auto i : s) // iterate over each and every elements
7
{
8
if (i == '(' or i == '{' or i == '[')
9
st.push(i); // if current element of the string will be opening bracket
10
// then we will just simply push it into the stack
11
else // if control comes to else part, it means that current element is a
12
// closing bracket, so check two conditions current element matches
13
// with top of the stack and the stack must not be empty...
14
{
15
if (st.empty() or (st.top() == '(' and i != ')') or (st.top() == '{' and i != '}') or
16
(st.top() == '[' and i != ']'))
17
return false;
18
st.pop(); // if control reaches to that line, it means we have got the
19
// right pair of brackets, so just pop it.
20
}
21
}
22
return st.empty(); // at last, it may possible that we left something into the
23
// stack unpair so return checking stack is empty or not..
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0