-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathBalanced Paranthesis.cpp
51 lines (47 loc) · 1.17 KB
/
Balanced Paranthesis.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
#include<bits/stdc++.h>
using namespace std;
unordered_map<char,int>symbol={ {'[',-1},{'(',-2},{'{',-3},{']',1},{')',2},{'}',3}};
void solve()
{
string s;
cin >> s;
stack<char>st;
for(auto bracket : s)
{
if(symbol[bracket]<0)
{
st.push(bracket);
}
else
{
if(st.empty())
{
cout << "NO\n";
return;
}
char top=st.top();
st.pop();
if(symbol[top]+symbol[bracket]!=0)
{
cout << "NO\n";
return;
}
}
}
if(st.empty())
{
cout << "YES\n";
return;
}
cout << "NO\n";
return;
}
int main()
{
int t;
cin >> t;
while(t--)
{
solve();
}
}