-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid_Parentheses.cpp
55 lines (51 loc) · 1.02 KB
/
Valid_Parentheses.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stack>
using namespace std;
bool isValid(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<char> sta;
for(auto i:s)
{
if (i == '[')
{
sta.push(i);
}
if (i == ']')
{
if (sta.size() == 0 || sta.top() != '[')
return false;
else
sta.pop();
}
if (i == '(')
{
sta.push(i);
}
if (i == ')')
{
if (sta.size() == 0 || sta.top() != '(')
return false;
else
sta.pop();
}
if (i == '{')
{
sta.push(i);
}
if (i == '}')
{
if (sta.size() == 0 || sta.top() != '{')
return false;
else
sta.pop();
}
}
return sta.size() == 0;
}
int main()
{
string s = "[]{}";
cout << isValid(s) << endl;
return 0;
}