Advertisement
Niloy007

Stack Problem

Oct 27th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6.     stack <char> stk;
  7.     string a;
  8.     cin >> a;
  9.  
  10.     if(a[0] == '(') {
  11.         for (int i = 0; i < a.length(); i++) {
  12.             if (a[i] == '(') {
  13.                 stk.push(a[i]);
  14.             } else if (a[i] == ')') {
  15.                 stk.pop();
  16.             }
  17.         }
  18.     } else if(a[0] == ')') {
  19.         return false;
  20.     }
  21.  
  22.  
  23.  
  24.     if(stk.size() == 0) {
  25.         cout << "Valid" << endl;
  26.     } else {
  27.         cout << "Invalid" << endl;
  28.     }
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement