-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidParentheses.cpp
47 lines (47 loc) · 1.08 KB
/
ValidParentheses.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
bool isValid(string s) {
stack<char> v;
for (char c : s) {
if (!v.empty()) {
char topChar = v.top();
switch (c) {
case ')':
if(topChar == '('){
v.pop();
break;
}
else{
v.push(')');
break;
}
case ']':
if(topChar == '['){
v.pop();
break;
}
else
{
v.push(']');
break;
}
case '}':
if(topChar == '{'){
v.pop();
break;
}
else
{
v.push('}');
break;
}
default:
v.push(c);
}
} else {
v.push(c);
}
}
if (v.size() == 0)
return true;
else
return false;
}