Josif_tepe

Untitled

Oct 15th, 2025
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <queue>
  4. #include <vector>
  5. #include <queue>
  6. #include <stack>
  7. using namespace std;
  8.  
  9. int main() {
  10.     string s;
  11.     cin >> s;
  12.    
  13.    
  14.     stack<char> st;
  15.    
  16.     bool is_valid = true;
  17.    
  18.     for(int i = 0; i < (int) s.size(); i++) {
  19.         char c = s[i];
  20.        
  21.         if(c == '(' or c == '[' or c == '{') {
  22.             st.push(c);
  23.         }
  24.         else {
  25.             if(st.empty()) {
  26.                 is_valid = false;
  27.                 break;
  28.             }
  29.             if(c == ')' and st.top() != '(') {
  30.                 is_valid = false;
  31.                 break;
  32.             }
  33.             if(c == ']' and st.top() != '[') {
  34.                 is_valid = false;
  35.                 break;
  36.             }
  37.             if(c == '}' and st.top() != '{') {
  38.                 is_valid = false;
  39.                 break;
  40.             }
  41.            
  42.             st.pop();
  43.         }
  44.     }
  45.    
  46.     if(is_valid and st.empty()) {
  47.         cout << "VALID" << endl;
  48.     }
  49.     else {
  50.         cout << "INVALID" << endl;
  51.     }
  52.    
  53.    
  54.    
  55.    
  56.    
  57.    
  58.  
  59.     return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment