-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
67 lines (56 loc) · 2.11 KB
/
solution.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
60
61
62
63
64
65
66
67
#include <iostream> // Header file for I/O
#include <stack> // Header file for using Stack
using namespace std;
int main() {
int test_cases,i;
cin>>test_cases;
while(test_cases--) {
string s;
cin>>s; // Taking the input string from the User
stack<char> stk; // Declaring a Stack of characters
for(i=0;i<s.length();i++) {
// If the current bracket is an Opening bracket , push it into the Stack
if(s[i]=='{'||s[i]=='('||s[i]=='[') {
stk.push(s[i]);
}
// If the current bracket is a Closing bracket
else {
// Try to match this Closing bracket with the corresponding Opening bracket
// If the Stack is empty , no Opening bracket exist , so the answer is NO
if(stk.empty()) {
break;
}
// The nature of this Closing bracket and Last Opened bracket must also match
// Otherwise the answer is NO
if(s[i]==')' && stk.top()!='(') {
break;
}
if(s[i]==']' && stk.top()!='[') {
break;
}
if(s[i]=='}' && stk.top()!='{') {
break;
}
// Remove the last Opened bracket
// This denotes that the current bracket is successfully matched.
stk.pop();
}
}
// If the loop did not run till s.length() then we have a mismatch
// Answer in such case is NO
if(i!=s.length()) {
cout<<"NO\n";
continue; // Continue with the next Test Case
}
// If the Stack is Empty then all brackets have been matched
if(stk.empty()) {
cout<<"YES\n";
}
// If the Stack is not Empty some brackets are left and are not matched
// Answer in such case is NO
else {
cout<<"NO\n";
}
}
return 0;
}