Skip to content

Commit 085f285

Browse files
LeetCode Problem 20. Valid Parentheses Problem Solved
1 parent 91dcb7d commit 085f285

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

20. Valid Parentheses.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution
5+
{
6+
public:
7+
bool isValid(string s)
8+
{
9+
stack<char> st;
10+
for (char c : s)
11+
{
12+
if (c == '(' || c == '{' || c == '[')
13+
{
14+
st.push(c);
15+
}
16+
else
17+
{
18+
if (st.empty())
19+
{
20+
return false;
21+
}
22+
else
23+
{
24+
if (c == ')' && st.top() == '(')
25+
{
26+
st.pop();
27+
}
28+
else if (c == '}' && st.top() == '{')
29+
{
30+
st.pop();
31+
}
32+
else if (c == ']' && st.top() == '[')
33+
{
34+
st.pop();
35+
}
36+
else
37+
{
38+
return false;
39+
}
40+
}
41+
}
42+
}
43+
return st.empty();
44+
}
45+
};

0 commit comments

Comments
 (0)