balanced brackets hackerrank java solution github code example
Example: balanced brackets hackerrank solution in cpp
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;
string isBalanced(string s){
stack <char> st;
for(auto c:s){
switch (c){
case '(':
case '{':
case '[':
st.push(c);
break;
case '}':
if(st.empty() || st.top()!='{' )
return "NO";
st.pop();
break;
case ']':
if(st.empty() || st.top()!='[')
return "NO";
st.pop();
break;
case ')':
if(st.empty() || st.top()!='(')
return "NO";
st.pop();
break;
default: break;
}
}
return st.empty() ? "YES":"NO";
}
int main(){
int t;
cin >> t;
for(int a0 = 0; a0 < t; a0++){
string s;
cin >> s;
cout << isBalanced(s) << endl;
}
return 0;
}