-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesisMatching.cpp
More file actions
80 lines (67 loc) · 1.21 KB
/
ParenthesisMatching.cpp
File metadata and controls
80 lines (67 loc) · 1.21 KB
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
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
struct Stack
{
int size;
int top;
char *s;
};
bool isEmpty(struct Stack stk)
{
return stk.top == -1;
}
bool isFull(struct Stack stk)
{
return stk.top == stk.size - 1;
}
void push(struct Stack *stk, char c)
{
stk->top++;
stk->s[stk->top] = c;
}
char pop(struct Stack *stk)
{
char c = stk->s[stk->top];
stk->top--;
return c;
}
int isBalanced(string str)
{
Stack stk;
stk.size = str.length();
stk.top = -1;
stk.s = new char[stk.size];
for(int i=0; i<str.length(); i++)
{
if(str[i]=='(' || str[i]=='[' || str[i]=='{')
{
push(&stk,str[i]);
}
else if(str[i] == ')' || str[i] == ']' || str[i] == '}')
{
if(isEmpty(stk))
{
return false;
}
else
{
pop(&stk);
}
}
}
return (isEmpty(stk)) ? true : false;
}
int main()
{
string str = "[{{({})([])})]";
int res = isBalanced(str);
switch (res)
{
case 1:
cout << "Balanced Parenthesis\n";
break;
case 0:
cout << "Not Balanced Parenthesis\n";
break;
}
}