-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson 7 - Brackets.cpp
40 lines (38 loc) · 1.04 KB
/
Lesson 7 - Brackets.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
/* ● Brackets
Determine whether a given string of parentheses (multiple types) is properly nested. */
#include <stack>
int solution(string &S) {
unsigned int i, n = S.length();
stack<char> nb;
for(i = 0; i < n; i++){
if(nb.empty() && (S[i] == ')' || S[i] == ']' || S[i] == '}')){
return 0;
}
switch (S[i]) {
case ')':
if(nb.top() == '('){
nb.pop();
}else {
nb.push(')');
}
break;
case '}':
if(nb.top() == '{'){
nb.pop();
}else {
nb.push('}');
}
break;
case ']':
if(nb.top() == '['){
nb.pop();
}else {
nb.push(']');
}
break;
default:
nb.push(S[i]);
}
}
return !(nb.size() > 0);
}