Balanced Parentheses Hackerrank Solution Java 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; }

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable