-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalancedBrackets.cpp
60 lines (49 loc) · 1.18 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 <bits/stdc++.h>
#include <deque>
using namespace std;
void checkParentheses(const string &parentheses)
{
std::deque<char> stack;
for (char elem: parentheses)
{
if ('{' == elem or '[' == elem or '(' == elem)
{
stack.push_front(elem);
} else
{
if (stack.empty())
{
std::cout << "NO" << std::endl;
return;
}
auto &front_elem = stack.front();
if (('}' == elem and '{' != front_elem) or
(']' == elem and '[' != front_elem) or
(')' == elem and '(' != front_elem))
{
std::cout << "NO" << std::endl;
return;
}
stack.pop_front();
}
}
if (not stack.empty())
{
std::cout << "NO" << std::endl;
return;
}
std::cout << "YES" << std::endl;
}
int main()
{
int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int t_itr = 0; t_itr < t; t_itr++)
{
string expression;
getline(cin, expression);
checkParentheses(expression);
}
return 0;
}