Skip to content

Commit 7aa3b70

Browse files
committed
valid_parentheses.cpp
1 parent f40b55f commit 7aa3b70

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

valid_parentheses.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
stack<char> chs;
5+
for (char ch : s)
6+
{
7+
if (ch == '(')
8+
chs.push(')');
9+
if (ch == '{')
10+
chs.push('}');
11+
if (ch == '[')
12+
chs.push(']');
13+
if (ch == ')' || ch == '}' || ch == ']')
14+
if (chs.empty() || chs.top() != ch)
15+
return false;
16+
else
17+
{
18+
chs.pop();
19+
}
20+
}
21+
if (chs.empty())
22+
return true;
23+
return false;
24+
}
25+
};

0 commit comments

Comments
 (0)