-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathtools.cpp
More file actions
45 lines (34 loc) · 1.11 KB
/
tools.cpp
File metadata and controls
45 lines (34 loc) · 1.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
#include <map>
#include <string>
#include "include/tools.h"
std::map<char, int> tools::count (std::string input) {
std::map<char, int> counter;
int length = input.length();
for(int i = 0; i < length; i++) {
if(input[i] == '(' || input[i] == '{' || input[i] == '[' || input[i] == '<') {
// count all the openers
counter[input[i]]++;
// count the closers
} else if(input[i] == ')') {
counter['(']--;
} else if(input[i] == '}') {
counter['{']--;
} else if(input[i] == ']') {
counter['[']--;
} else if(input[i] == '>') {
counter['<']--;
}
if(counter['('] < 0 || counter['{'] < 0 || counter['['] < 0 || counter['<'] < 0) {
// Right side closer without a pair
counter.clear();
return counter;
}
}
return counter;
}
bool tools::verify_closers(std::map<char, int> counter) {
if(counter.empty() || counter ['<'] != 0 || counter['{'] != 0 || counter['('] != 0 || counter['['] != 0 ) {
return false;
}
return true;
}