Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to to, let us concentrate rather on explaining to human beings what we want a computer to do. Donald Knuth

Balance symbol

Language Java | Level Intermediate | Category Data structure | August 28, 2015 12:54 am


Data structure Description

Write a program to read the symbol string from the user. The program should validate the symbols using the stack. Stack uses push and pop operations for validating the symbol.

Code editor validates the syntax and gives error based on the symbol balancing.

Example
{[()]} - Balanced symbol.
{(]} - Not balanced symbol


Output

   					 

[(12*45)]{12+45}
true

{(34*56)+23(45)
False


				    

Comments


Posted by gita.magani
March 7, 2016 1:45 pm

When I declared stack as private static one of my conditions failed. String bsy1 = "{()){}"; // Gave false String bsy2 = "{(){}}"; // Gave True String bsy3 = "{()[}}"; // Gave false String bsy4 = "{(){}}"; // Gave false, which is incorrect. So I moved the line private static Stack < Character > stack = new Stack < Character >(); into isBalanced method as Stack stack = new Stack(); which gave me expected result.


Please login to add comments.