-
Notifications
You must be signed in to change notification settings - Fork 77
/
BalancedBrackets.cpp
59 lines (55 loc) · 1.27 KB
/
BalancedBrackets.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
52
53
54
55
56
57
58
59
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int tc;
string s;
string::iterator it;
map<char, char> p;
p['{'] = '}';
p['['] = ']';
p['('] = ')';
cin>>tc;
while(tc) {
cin>>s;
//cout<<"string"<<s<<endl;
int i = 0;
stack<char> st;
for (it=s.begin(); it!=s.end(); it++) {
//cout<<s[i];
if(s[i] == '{' || s[i] == '[' || s[i] == '(') {
//cout<<"push"<<endl;
st.push(s[i]);
i++;
}
else if(!st.empty()){
char c = st.top();
//cout<<"pop"<<c<<endl;
if(p[c] == s[i]) {
st.pop();
i++;
}
else {
break;
}
} else {
break;
}
}
if(st.empty() && it == s.end()) {
cout<<"YES"<<endl;
} else {
cout<<"NO"<<endl;
}
//cout<<endl;
tc--;
}
return 0;
}