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

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?