forked from Anuragcoderealy/Codenewhacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalanced_Parenthesis.cpp
48 lines (48 loc) · 1.07 KB
/
Balanced_Parenthesis.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
#include <iostream>
#include <stack>
using namespace std;
bool isBalancedExp(string exp)
{
stack<char> stk;
char x;
for (int i = 0; i < exp.length(); i++)
{
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
{
stk.push(exp[i]);
continue;
}
if (stk.empty())
return false;
switch (exp[i])
{
case ')':
x = stk.top();
stk.pop();
if (x == '{' || x == '[')
return false;
break;
case '}':
x = stk.top();
stk.pop();
if (x == '(' || x == '[')
return false;
break;
case ']':
x = stk.top();
stk.pop();
if (x == '(' || x == '{')
return false;
break;
}
}
return (stk.empty());
}
int main()
{
string expresion = "()[(){()}]";
if (isBalancedExp(expresion))
cout << "This is Balanced Expression";
else
cout << "This is Not Balanced Expression";
}