forked from gutty333/Medium-Programming-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
30_MultipleBrackets.cpp
74 lines (65 loc) · 2 KB
/
30_MultipleBrackets.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
72
73
74
// For this challenge you will determine if all the brackets in a string are correctly matched up.
// have the function MultipleBrackets(str) take the str parameter being passed and return 1 #ofBrackets if the brackets are correctly matched and each one is accounted for. Otherwise return 0. For example: if str is "(hello [world])(!)", then the output should be 1 3 because all the brackets are matched and there are 3 pairs of brackets, but if str is "((hello [world])" the the output should be 0 because the brackets do not correctly match up. Only "(", ")", "[", and "]" will be used as brackets. If str contains no brackets return 1.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string MultipleBrackets(string str) {
int count = 0, count2 = 0;
int total = 0;
// Overall this is quite similar to the previous bracket matcher problem
for (int x = 0; x < str.length(); x++)
{
// Checking for the ( opening bracket
if (str[x] == '(')
{
count++;
}
else if (str[x] == '[') // Checking for the [ bracket
{
count2++;
}
else if (str[x] == ')') // Check that () brackets open and close
{
count--;
total++;
if (count < 0)
{
return "0";
}
}
else if (str[x] == ']') // Check that [] brackets open and close
{
count2--;
total++;
if (count2 < 0)
{
return "0";
}
}
}
if (count == 0 && count2 == 0 && total == 0)
{
return "1";
}
else if (count == 0 && count2 == 0 && total > 0)
{
stringstream convert;
convert << 1 << " " << total;
return convert.str();
}
else
{
return "0";
}
}
int main() {
// keep this function call here
cout << MultipleBrackets("(coder)[byte)]") << endl; // 0
cout << MultipleBrackets("(c([od]er)) b(yt[e])") << endl; // 1 5
cout << MultipleBrackets("(hello [world])(!)") << endl; // 1 3
cout << MultipleBrackets("((hello [world])") << endl; // 0
cout << MultipleBrackets("Attack on titan is awesome") << endl; // 1
cout << MultipleBrackets("()code[rb]yte() yes()[ss][[") << endl; // 0
return 0;
}