SHARE
TWEET

CHECKPARENTHESES

a guest Apr 24th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main () {
  5.     int i;
  6.     string s;
  7.     stack<char> check;
  8.     getline(cin, s);
  9.     for (i = 0; i < s.length(); ++i) {
  10.         if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
  11.             check.push(s[i]);
  12.         }
  13.         else if (s[i] == ')' || s[i] == ']' || s[i] == '}') {
  14.             if (check.empty())
  15.                 break;
  16.             char c = check.top();
  17.             check.pop();
  18.             if (s[i] == ')' && c != '('
  19.                 || s[i] == ']' && c != '['
  20.                 || s[i] == '}' && c != '{') {
  21.                 break;
  22.             }
  23.         }
  24.     }
  25.     if (i < s.length() || !check.empty())
  26.         printf("Invalid\n");
  27.     else
  28.         printf("Valid\n");
  29.  
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top