Sreekar_0125

Untitled

Sep 13th, 2022
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. char stack[1000000];
  4. int top=-1;
  5. void push(char ch) {
  6.     top++;
  7.     stack[top] = ch;
  8. }
  9. void pop () {
  10.     top--;
  11. }
  12.  
  13. int main () {
  14.     string e;
  15.     cin >> e;
  16.     int flag=0;
  17.     for(int i=0;i<e.length();i++) {
  18.         if(e[i] == '{' || e[i] == '(' || e[i] == '[') {
  19.             push(e[i]);
  20.         }
  21.         else if (e[i] == '}') {
  22.             if(stack[top]!='{') {
  23.                 flag = 1;
  24.                 break;
  25.             }
  26.             else {
  27.                 pop();
  28.             }
  29.         }
  30.         else if(e[i] == ')') {
  31.             if(stack[top]!='(') {
  32.                 flag=1;
  33.                 break;
  34.             }
  35.             else {
  36.                 pop();
  37.             }
  38.         }
  39.         else if(e[i] == ']') {
  40.             if(stack[top]!='[') {
  41.                 flag=1;
  42.                 break;
  43.             }
  44.             else {
  45.                 pop();
  46.             }
  47.         }
  48.     }
  49.    
  50.     if(top!=-1) {
  51.         flag=1;
  52.     }
  53.     if(flag) {
  54.         cout << e << endl;
  55.         cout << "NOT BALANCED" << endl;
  56.     }
  57.     else {
  58.         cout << e << endl;
  59.         cout << "BALANCED" << endl;
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment