-
Notifications
You must be signed in to change notification settings - Fork 381
/
Balanced Paranthesis.txt
70 lines (58 loc) · 1.36 KB
/
Balanced Paranthesis.txt
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
69
70
#include <iostream>
#include<stack>
using namespace std;
int length(char *exp){
int length=0;
for(int i=0;exp[i]!='\0';i++)
length++;
return length;
}
bool checkBalanced(char *exp){
stack<int> st;
int l =length(exp);
for(int i = 0 ; i < l ; i++){
if(exp[i]=='('||exp[i]=='{'||exp[i]=='['){
st.push(exp[i]);
continue;
}
else if(exp[i]==')'){
if(st.empty() == false){
if(st.top()=='(')
st.pop();
}
else
return false;
}
else if(exp[i]=='}'){
if(st.empty() == false){
if(st.top()=='{')
st.pop();
}
else
return false;
}
else if(exp[i]==']'){
if(st.empty() == false){
if(st.top()=='[')
st.pop();
}
else
return false;
}
}
if(st.empty()==true){
return true;
}
else
return false;
}
int main() {
char input[100000];
cin.getline(input, 100000);
if(checkBalanced(input)) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}