We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I added some comments for those having some trouble understanding what's happening.
boolis_balanced(stringexpression){stack<char>s;//stack of right bracket expectationsfor(charc:expression){switch(c){case'(':s.push(')');break;case'[':s.push(']');break;case'{':s.push('}');break;default:// once you have iterated passed the left brackets// compare right bracket with the one expectedif(s.empty()||c!=s.top())returnfalse;s.pop();// it matches, so remove it}}returns.empty();// stack must be even// this checks for leftover right brackets in the stack// the s.empty() in default checks for excess left brackets}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Stacks: Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
I added some comments for those having some trouble understanding what's happening.