-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackParenthesis.cpp
68 lines (54 loc) · 1.17 KB
/
stackParenthesis.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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
char input[1000];
int Stack[100], ind;
void push(int x){
++ind;
Stack[ind] = x;
}
bool isEmpty(){
if(ind >= 1) return false;
else return true;
}
void pop(){
Stack[ind] = 0;
ind--;
}
int top(){
return Stack[ind];
}
bool verify(char input[]){
ind = 0;
int n = strlen(input);
for(int i = 0; i<n; i++){
if(input[i] == '(') push(1);
if(input[i] == '{') push(2);
if(input[i] == '[') push(3);
if(input[i] == ')'){
if(isEmpty() || Stack[ind] != 1) return false;
else {
pop();
}
}
if(input[i] == '}'){
if(isEmpty() || Stack[ind] != 2) return false;
else {
pop();
}
}
if(input[i] == ']'){
if(isEmpty() || Stack[ind] != 3) return false;
else {
pop();
}
}
}
if(isEmpty()) return true;
else return false;
}
int main(){
cin>>input;
cout<<verify(input);
return 0;
}