Software sucks because users demand it to. Nathan Myhrvold

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.

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







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.