-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson 7 - Brackets.c
41 lines (39 loc) · 1.07 KB
/
Lesson 7 - Brackets.c
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
/* ● Brackets
Determine whether a given string of parentheses (multiple types) is properly nested. */
int solution(char *S) {
unsigned int len = strlen(S), i;
int top = -1;
char stack[len];
for (i = 0; i < len; i++) {
if (top ==-1 && (S[i] == ')' || S[i] == '}' || S[i] == ']')) {
return 0;
}
switch (S[i]) {
case '}':
if (stack[top] != '{') {
return 0;
}else {
top--;
}
break;
case ']':
if (stack[top] != '[') {
return 0;
}else {
top--;
}
break;
case ')':
if (stack[top] != '(') {
return 0;
}else {
top--;
}
break;
default:
top++;
stack[top] = S[i];
}
}
return top == -1;
}