-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancedbrackets.cpp
More file actions
123 lines (100 loc) · 3.7 KB
/
balancedbrackets.cpp
File metadata and controls
123 lines (100 loc) · 3.7 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
HackerRank's Stacks: Balanced Brackets
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].
Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and ().
A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example, {[(])} is not balanced because the contents in between { and } are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket, (, and the pair of parentheses encloses a single, unbalanced closing square bracket, ].
By this logic, we say a sequence of brackets is considered to be balanced if the following conditions are met:
It contains no unmatched brackets.
The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.
Given strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, print YES on a new line; otherwise, print NO on a new line.
Input Format
The first line contains a single integer, , denoting the number of strings.
Each line of the subsequent lines consists of a single string, , denoting a sequence of brackets.
Constraints
, where is the length of the sequence.
Each character in the sequence will be a bracket (i.e., {, }, (, ), [, and ]).
Output Format
For each string, print whether or not the string of brackets is balanced on a new line. If the brackets are balanced, print YES; otherwise, print NO.
Sample Input
3
{[()]}
{[(])}
{{[[(())]]}}
Sample Output
YES
NO
YES
Explanation
The string {[()]} meets both criteria for being a balanced string, so we print YES on a new line.
The string {[(])} is not balanced, because the brackets enclosed by the matched pairs [(] and (]) are not balanced. Thus, we print NO on a new line.
The string {{[[(())]]}} meets both criteria for being a balanced string, so we print YES on a new line.
Submissions: 11840
Max Score: 30
Difficulty: Medium
*/
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
bool is_balanced(string expression) {
int len = expression.length();
if (len == 0 || len % 2 != 0)
return false;
stack<char> bstack;
for (int i=0; i<len; i++) {
if (expression[i] == '(') {
bstack.push(')');
} else if (expression[i] == '[') {
bstack.push(']');
} else if (expression[i] == '{') {
bstack.push('}');
} else {
char b;
// if the stack is empty then the string is not bracket balanced
if (bstack.empty())
return false;
// the top item should match the next item in the string
b = bstack.top();
bstack.pop();
if (b != expression[i])
return false;
}
}
// last ensure the stack is empty of any opening brackets
if (!bstack.empty())
return false;
else
return true;
}
int main(){
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
string expression;
cin >> expression;
bool answer = is_balanced(expression);
if(answer)
cout << "YES\n";
else cout << "NO\n";
}
return 0;
}