-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11 STACK_APPLICATION.cpp
More file actions
70 lines (55 loc) · 1.6 KB
/
11 STACK_APPLICATION.cpp
File metadata and controls
70 lines (55 loc) · 1.6 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
/*
Radha asked to her friend can you verify that given expression have balanced parenthesis or not, So her decided to write a code to examine that the given an string expression whether have the pairs of parenthesis are balanced or not. The orders of “(“, “)”, “[“, “]” ,“{“, “}”,are correct in the given expression.
Sample 1: Line 1: Enter the expression : {}{[()]} Line 2: Balanced
Sample 2: Line 1 : Enter the expression : [(]) Line 2 : Not Balanced
Input Format
User enter the paris of parentheses without any operand.
Constraints
String expression length should > 0
Output Format
The result will display as parenthesis is balanced or not.
Sample Input 0
(){}[][{()()}]{()}
Sample Output 0
Balanced
Explanation 0
1: Enter the expression : (){}[][{()()}]{()} Balanced
Enter the expression : ({}[]}] Not Balanced
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
stack<char> bracket;
void check(string s)
{
int n= s.length();
for(int i=0;i<n;i++)
{
if(bracket.empty())
{
bracket.push(s[i]);
}
else if((bracket.top()=='{' && s[i]=='}')||(bracket.top()=='(' && s[i]==')')||(bracket.top()=='[' && s[i]==']'))
{
bracket.pop();
}
else{
bracket.push(s[i]);
}
}
if(bracket.empty())
cout<<"Balanced";
else
cout<<"Not Balanced";
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string br;
cin>>br;
check(br);
return 0;
}