-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding027.cpp
65 lines (50 loc) · 1.32 KB
/
dailycoding027.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
#include <iostream>
#include <stack>
#include <string>
#include <map>
using std::map;
using std::cout;
using std::endl;
using std::string;
using std::stack;
map<char, char> getParenMap() {
map<char, char> m;
m[')'] = '(';
m[']'] = '[';
m['}'] = '{';
return m;
}
bool validate(string str) {
// initialize the stack to hold left facing parentheses
stack<char> parenStack;
map<char, char> parenMap = getParenMap();
// base case: initially invalid
if (parenMap.find(str[0]) != parenMap.end()) return false;
/*
loop through the string and check if each char is in the map
1) if so, check if it matches the top of the stack
2) otherwise, add it to the stack
*/
for (char i: str) {
if (parenMap.find(i) == parenMap.end()) {
parenStack.push(i);
} else if (parenStack.top() == parenMap[i]) {
parenStack.pop();
} else {
return false;
}
}
// the string was valid if the stack ends up empty
return parenStack.empty();
}
int main() {
string tests[6] = {"([])[]({})", "([)]", "]{[()]}}", "[][]", "[(})]", "[{}](()"};
int results[6] = {1, 0, 0, 1, 0, 0};
bool test_result = true;
for (int i = 0; i < 6; i++) {
test_result &= (validate(tests[i]) == results[i]);
}
if (test_result) cout << "Passed" << endl;
else cout << "Failed" << endl;
return 0;
}