-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbalancedBrackets.cpp
More file actions
71 lines (67 loc) · 1.46 KB
/
balancedBrackets.cpp
File metadata and controls
71 lines (67 loc) · 1.46 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
/*
1. You are given a string exp representing an expression.
2. You are required to check if the expression is balanced
i.e. closing brackets and opening brackets match up well.
e.g.
[(a+b)+{(c+d)*(e/f)}] -> true
[(a + b) + {(c + d) * (e / f)]} -> false
[(a + b) + {(c + d) * (e / f)} -> false
([(a + b) + {(c + d) * (e / f)}] -> false
*/
#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
#include<utility>
#include<unordered_map>
#include<set>
#include<map>
#include<unordered_set>
#include<string>
#include<limits.h>
using namespace std;
#define ll long long int
const int mod=1e9+7;
bool isBalanced(string &str)
{
stack<char> st;
for(int i=0;i<str.size();i++)
{
char ch=str[i];
if(ch=='(' || ch=='{' || ch=='[')
{
st.push(ch);
}
else if(ch==')' || ch=='}' || ch==']')
{
if(!st.empty())
{
if(ch==')' && st.top()!='(')
return false;
else if(ch=='}' && st.top()!='{')
return false;
else if(ch==']' && st.top()!='[')
return false;
st.pop();
}
else
return false;
}
}
if(!st.empty())
return false;
return true;
}
int main()
{
string s;
getline(cin,s);
if(isBalanced(s))
cout<<"true\n";
else
cout<<"false\n";
}