Balanced Bracket Problem Method 2 (Integer assigned Method)

PHOTO EMBED

Sat Jul 02 2022 17:45:31 GMT+0000 (UTC)

Saved by @BRIJESH20 #c++

// Balanced Bracket Problem Method2.

#include<bits/stdc++.h>
using namespace std ;


bool isBalanced(string str)
{
    map<char, int> m ;
    m['('] = -1 ; m['{'] = -2 ; m['['] = -3 ; m[')'] = 1 ; m['}'] = 2 ; m[']'] = 3 ; 
    stack<int> s; 
    for(int i=0; i<str.length(); i++){
        if(m[str[i]] < 0) s.push(m[str[i]]) ;
        else {
            if(s.empty()) return false ;
            else if(m[str[i]]+s.top() != 0) return false ;
            else s.pop() ;
        }
    }
    return (s.empty()) ;
}

int main()
{
    string s ; cin >> s ;
    cout << isBalanced(s) << endl ;
    
    
    return 0 ;
}
content_copyCOPY

Balanced Bracket Problem Method 2 (Integer assigned Method)