-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.cpp
More file actions
87 lines (81 loc) · 2.11 KB
/
brackets.cpp
File metadata and controls
87 lines (81 loc) · 2.11 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
// you can use includes, for example:
// #include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int squigly_bracket = 0;
int square_bracket = 0;
int round_bracket = 0;
char latest = '0';
if(S.empty())
return 1;
else
{
int x = 0;
while((squigly_bracket >= 0)&&(square_bracket >= 0)&&(round_bracket >= 0))
{
if((int)S[x] == 40)
{
round_bracket++;
latest = '(';
//break;
}
if((int)S[x] == 41)
{
round_bracket--;
if((latest != '(')&&(latest != '0'))
break;
else
latest = '0';
//break;
}
if((int)S[x] == 123)
{
squigly_bracket++;
latest = '{';
//break;
}
if((int)S[x] == 125)
{
squigly_bracket--;
if((latest != '{')&&(latest != '0'))
break;
else
latest = '0';
//break;
}
if((int)S[x] == 91)
{
square_bracket++;
latest = '[';
//break;
}
if((int)S[x] == 93)
{
square_bracket--;
if((latest != '[')&&(latest != '0'))
break;
else
latest = '0';
}
if(x == strlen(S.c_str()))
{
if((square_bracket == 0)&&(squigly_bracket == 0)&&(round_bracket == 0))
{
return 1;
}
else
{
return 0;
}
}
x++;
}
//loop through string
return 0;
}
}