blob: 738b76997842c0c2a96d2146ef9df7dece92aa04 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include <string>
#include <stack>
class Solution
{
public:
bool isValid(std::string s)
{
std::stack<char> open;
for (char ch : s)
{
if (ch == '(' || ch == '{' || ch == '[')
{
open.push(ch);
}
else
{
if (open.empty())
return false;
if ((open.top() == '(' && ch == ')') ||
(open.top() == '{' && ch == '}') ||
(open.top() == '[' && ch == ']'))
{
open.pop();
continue;
}
return false;
}
}
return open.empty();
}
};
|