Skip to content

Commit 54a152a

Browse files
committed
Time: 3 ms (46.87%), Space: 6.3 MB (50.94%) - LeetHub
1 parent e50c93d commit 54a152a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
bool isValid(string str) {
4+
// (', ')', '{', '}', '[' and ']'
5+
int n = str.size();
6+
stack<char> st;
7+
8+
for (int i = 0; i < n; i++) {
9+
char c = str[i];
10+
if (c == '(' or c == '{' or c == '[')
11+
st.push(c);
12+
else if (st.empty() || (c == ')' and st.top() != '(') || (c == ']' and st.top() != '[') || (c == '}') and st.top() != '{')
13+
return false;
14+
else
15+
st.pop();
16+
}
17+
18+
return st.empty();
19+
}
20+
};
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+

0 commit comments

Comments
 (0)