-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbalanced_parenthesis.cpp
More file actions
42 lines (34 loc) · 847 Bytes
/
Copy pathbalanced_parenthesis.cpp
File metadata and controls
42 lines (34 loc) · 847 Bytes
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
#include <stack>
bool checkBalanced(char *exp) {
// Write your code here
stack <char> s;
for(int i=0;exp[i]!='\0';i++)
{
if(exp[i]=='[' || exp[i]=='(' || exp[i]=='{' )
s.push(exp[i]);
if(s.empty() && (exp[i]==']' || exp[i]==')' || exp[i]=='}') )
return false;
if(exp[i]==']')
{ if(s.top()=='[')
s.pop();
else
return false;
}
if(exp[i]==')')
{ if(s.top()=='(')
s.pop();
else
return false;
}
if(exp[i]=='}')
{ if(s.top()=='{')
s.pop();
else
return false;
}
}
if(s.empty())
return true ;
else
return false;
}