-
Notifications
You must be signed in to change notification settings - Fork 0
/
balanced_brackets.cpp
54 lines (43 loc) · 1.01 KB
/
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
52
53
54
#include <bits/stdc++.h>
using namespace std;
// Complete the isBalanced function below.
string isBalanced(string s) {
char arr1[] = {'(','[','{'};
char arr2[] = {')',']','}'};
stack<int> stack;
for(int i=0; i<s.length(); i++){
if( !stack.empty() && stack.top() == arr1[0] && s[i]== arr2[0]){
stack.pop();
}
else if( !stack.empty() && stack.top() == arr1[1] && s[i] == arr2[1]){
stack.pop();
}
else if(!stack.empty() && stack.top()== arr1[2] && s[i] == arr2[2]){
stack.pop();
}
else{
stack.push(s[i]);
}
}
if(stack.empty()){
return "YES";
}
else{
return "NO";
}
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int t_itr = 0; t_itr < t; t_itr++) {
string s;
getline(cin, s);
string result = isBalanced(s);
fout << result << "\n";
}
fout.close();
return 0;
}