This famous coding interview project explains how to balance a string of brackets in Java using stack
If for every opening bracket there is a corresponding closing bracket for it, then that string is called balanced.
Example : Balanced string : {[]} , ({})
Not balanced string : {)), []{{
Logic of the program: Using Push function take input of the opening brackets
Using the pop function remove the opening brackets having respective closing brackets.
These steps get repeated until we reach a point where the stack is either empty, or some elements are still present. If the stack remains empty, we print the value "balanced". If after all the iteration the stack is not empty then we print "not balanced".
Submitted by Anirban Chatterjee (anirban20)
Download packets of source code on Coders Packet
Comments