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.
Aha: THAT's the clever answer I should've gone for. Beats my straightforward approach:
publicstaticbooleanisBalanced(Stringexpression){Stack<Character>stack=newStack<>();for(charc:expression.toCharArray()){switch(c){case'{':case'[':case'(':stack.push(c);break;case'}':if(stack.isEmpty()||stack.pop()!='{')returnfalse;break;case']':if(stack.isEmpty()||stack.pop()!='[')returnfalse;break;case')':if(stack.isEmpty()||stack.pop()!='(')returnfalse;break;}// end switch }// end for if(!stack.isEmpty())returnfalse;returntrue;}
Stacks: Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
Aha: THAT's the clever answer I should've gone for. Beats my straightforward approach: