forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(HACKERRANK) Balanced Brackets.cpp
71 lines (56 loc) · 2.09 KB
/
(HACKERRANK) 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
//This ALgorithm checks whether all the brackets of string are balanced or not
//Go to Hackerrank "Balanced Brackets" Question to see more description
// Complete the isBalanced function below.
string isBalanced(string text) {
//creating a stack to store opening brackets only
stack <char> opening_brackets_stack;
//Looping till string end
for (int position = 0; position < text.length(); ++position) {
char next = text[position];
//storing the character in a next named char
//if it is open bracket then simpy push it in stack
if (next == '(' || next == '[' || next == '{') {
opening_brackets_stack.push(next);
}
//if closed then check two conditions
if (next == ')' || next == ']' || next == '}') {
char last;
//if the stack is empty means brackets will be unbalanced if not then it get the last bracket of stack
if(!opening_brackets_stack.empty())
{
last = opening_brackets_stack.top();
}
else return "NO";
//if last matches with next then pop that opening bracket otherwise return NO
if ((last == '(' && next== ')') || (last == '[' && next == ']') || (last == '{' && next== '}'))
{
opening_brackets_stack.pop();
continue;
}
else
return "NO";
}
}
//At the end if nothing is returned from loop then now check whether the stack is empty or not if yes then it means, string was balanced.
if(opening_brackets_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;
}