Advertisement
LEGEND2004

Bracket Sequence

Feb 10th, 2023 (edited)
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int  main(){
  4.     string s;
  5.     cin >> s;
  6.     int n = s.size();
  7.     bool f = true;
  8.     int cnt = 0;
  9.     for(int i = 0; i < n; i++){
  10.         if(s[i] == '(')
  11.             cnt++;
  12.         else{
  13.             cnt--;
  14.             if(cnt < 0){
  15.                 f = false;
  16.             }
  17.         }
  18.     }
  19.     if(cnt > 0)
  20.         f = false;
  21.     if(f){
  22.         cout << "YES" << endl;
  23.     }else{
  24.         cout << "NO" << endl;
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement