forked from piyush-kash/Hacktober2021-cpp-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balanced_brackets.cpp
51 lines (48 loc) · 927 Bytes
/
balanced_brackets.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
49
50
51
//CHEQUE on Balanced Brackets
#include <iostream>
#include <stack>
using namespace std;
bool isbalance(string s)
{
stack<char> st;
for (int it = 0; it < s.size(); it++)
{
if (s[it] == '(' || s[it] == '{' || s[it] == '[')
{
st.push(s[it]);
}
else
{
if (st.empty())
{
return false;
}
char x = st.top();
st.pop();
if (x == '(' && s[it] == ')' || x == '[' && s[it] == ']' || x == '{' && s[it] == '}')
{
continue;
}
else
{
return false;
}
}
}
return st.empty();
}
int main()
{
string s;
cout << "Enter Strings of brackets : ";
cin >> s;
if (isbalance(s))
{
cout << "TRUE";
}
else
{
cout << "FALSE";
}
return 0;
}