Advertisement
jbn6972

Untitled

Oct 26th, 2022
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. // Code Written by : John Nixon
  2. // Date: 26:10:2022  Time: 11:17:37
  3. // Copyrights are applicable
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. #define ll long long
  7. #define vi vector<int>
  8. #define vll vector<long long int>
  9.  
  10.  
  11. // check if balanced backets are present or not
  12. bool checkBalanced(string s){
  13.     stack<char> st;
  14.     for(int i=0;i<s.length();i++){
  15.         if(s[i]=='(' || s[i]=='{' || s[i]=='['){
  16.             st.push(s[i]);
  17.         }
  18.         else if(s[i]==')'){
  19.             if(st.empty() || st.top()!='('){
  20.                 return false;
  21.             }
  22.             st.pop();
  23.         }
  24.         else if(s[i]=='}'){
  25.             if(st.empty() || st.top()!='{'){
  26.                 return false;
  27.             }
  28.             st.pop();
  29.         }
  30.         else if(s[i]==']'){
  31.             if(st.empty() || st.top()!='['){
  32.                 return false;
  33.             }
  34.             st.pop();
  35.         }
  36.     }
  37.     if(st.empty()){
  38.         return true;
  39.     }
  40.     return false;
  41. }
  42. int main()
  43. {
  44. #ifndef ONLINE_JUDGE
  45.     freopen("input.txt", "r", stdin);
  46.     freopen("output.txt", "w", stdout);
  47. #endif
  48.     std::ios::sync_with_stdio(false);
  49.     string str;
  50.     cin>>str;
  51.     if(checkBalanced(str)){
  52.         cout<<"Balanced";
  53.     }
  54.     else{
  55.         cout<<"Not Balanced";
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement