-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
30 lines (28 loc) · 702 Bytes
/
Copy pathstack.cpp
File metadata and controls
30 lines (28 loc) · 702 Bytes
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
#include <iostream>
#include <queue>
#include <stack>
#include <string>
int isBalanced(std::string a) {
std::stack<char> st;
for (char element : a) {
if (element == '{' || element == '[' || element == '(') {
st.push(element);
} else {
if (st.empty()) {
return 0;
} else if ((element == '}' && st.top() != '{') ||
(element == ')' && st.top() != '(') ||
(element == ']' && st.top() != '[')) {
return 0;
} else {
st.pop();
}
}
}
return 1;
}
int main(int argc, char const *argv[]) {
std::string test = "()[]{}}";
std::cout << (isBalanced(test) ? "True" : "False") << std::endl;
return 0;
}