Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- char stack[1000000];
- int top=-1;
- void push(char ch) {
- top++;
- stack[top] = ch;
- }
- void pop () {
- top--;
- }
- int main () {
- string e;
- cin >> e;
- int flag=0;
- for(int i=0;i<e.length();i++) {
- if(e[i] == '{' || e[i] == '(' || e[i] == '[') {
- push(e[i]);
- }
- else if (e[i] == '}') {
- if(stack[top]!='{') {
- flag = 1;
- break;
- }
- else {
- pop();
- }
- }
- else if(e[i] == ')') {
- if(stack[top]!='(') {
- flag=1;
- break;
- }
- else {
- pop();
- }
- }
- else if(e[i] == ']') {
- if(stack[top]!='[') {
- flag=1;
- break;
- }
- else {
- pop();
- }
- }
- }
- if(top!=-1) {
- flag=1;
- }
- if(flag) {
- cout << e << endl;
- cout << "NOT BALANCED" << endl;
- }
- else {
- cout << e << endl;
- cout << "BALANCED" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment