Advertisement
Spocoman

07. Balanced Parenthesis

Jan 16th, 2024
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     string str;
  8.     cin >> str;
  9.  
  10.     stack<char> openBrackets;
  11.  
  12.     for (char c : str) {
  13.         if (c == '(' || c == '[' || c == '{') {
  14.             openBrackets.push(c);
  15.         }
  16.         else if (openBrackets.size() == 0 ||
  17.             c == ')' && openBrackets.top() != '(' ||
  18.             c == ']' && openBrackets.top() != '[' ||
  19.             c == '}' && openBrackets.top() != '{') {
  20.             cout << "NO\n";
  21.             return 0;
  22.         }
  23.         else {
  24.             openBrackets.pop();
  25.         }
  26.     }
  27.  
  28.     cout << "YES\n";
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement