-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_braces.cpp
More file actions
46 lines (40 loc) · 1 KB
/
Copy pathvalid_braces.cpp
File metadata and controls
46 lines (40 loc) · 1 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
#include <iostream>
#include <vector>
#include <cstring>
void TrimRight(char *s) {
if (s == nullptr)
return;
size_t length = strlen(s);
if (length == 0)
return;
char *LastNonSpace = s + length - 1;
while (LastNonSpace >= s && *LastNonSpace == ' ')
--LastNonSpace;
*(LastNonSpace + 1) = '\0';
}
bool valid_braces(std::string braces) {
std::vector<char> queue;
char last = ' ';
for (char c : braces) {
if (c == '(' || c == '{' || c == '[')
queue.push_back(c);
else {
last = queue.back();
queue.pop_back();
if (last == '(' && c == ')') {
continue;
} else if (last == '{' && c == '}')
continue;
else if (last == '[' && c == ']')
continue;
else
return false;
}
}
return queue.size() == 0;
}
int main() {
std::string a = "(){}[]";
std::cout << valid_braces(a);
return 0;
}