Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <stack>
- using namespace std;
- int main() {
- string x;
- stack < char, vector<char> > iStack;
- cout << "Enter series of parentheses: ";
- getline(cin, x);
- char ch;
- int cnt = 0;
- for (int i = 0; i < x.length(); i++) {
- if (x[i] == '(' || x[i] == '{' || x[i] == '[') {
- cnt++;
- iStack.push(x[i]);
- } else {
- if (iStack.empty()) {
- break;
- }
- if (x[i] == ')') {
- if (iStack.top() == '(') {
- cnt++;
- iStack.pop();
- }
- } else if (x[i] == '}') {
- if (iStack.top() == '{') {
- cnt++;
- iStack.pop();
- }
- } else if (x[i] == ']') {
- if (iStack.top() == '[') {
- cnt++;
- iStack.pop();
- }
- }
- }
- }
- if (iStack.empty() && cnt == (int) x.size()) {
- cout << "Balance!" << endl;
- } else {
- cout << "Not Balance!" << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment