-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
39 lines (31 loc) · 697 Bytes
/
stack.cpp
File metadata and controls
39 lines (31 loc) · 697 Bytes
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
/*
'(, [, {'
'(, [, {'
'()[]'
*/
#include<bits/stdc++.h>
using namespace std;
bool sol(string s)
{
stack<char> st;
int i = 0, n = s.length();
while(i < n)
{
if(st.empty() && (s[i] == ')' || s[i] == ']' || s[i] == '}')) return 0;
if(s[i] == ')' && st.top() == '(') st.pop();
else if(s[i] == ']' && st.top() == '[') st.pop();
else if(s[i] == '}' && st.top() == '{') st.pop();
else if(s[i] == '(' || s[i] == '[' || s[i] == '{') st.push(s[i]);
else return 0;
i++;
}
if(!st.empty()) return 0;
return true;
}
int main()
{
string s;
cin>>s;
if(sol(s)) cout<<"Balanced";
else cout<<"Unbalanced";
}