Posts

Finding whether a given string has balanced parenthesis using Java Stack

Image
Challenge: Given a string in the stdin with a set of parenthesis [{[()]}] and the stdout should print either true or false based on whether the parenthesis are matched or not e.g:  {} --> true {[()]} -->true {[}] -- > false Solution : I am going to use a Java Stack to resolve this problem following the below approach. 1.use stdin to read the input 2.iterate reading from stdin while there are more inputs 3.store each string in a variable and validate 4.validation >>      a. loop through each character of the string     b.If current character is ( OR { OR [ then add the value to the Stack     c.If the current character is ) OR } OR ] then pop a value from the Stack and compare it with the current character    d. if c matches then result is true , continue ; else set result as false  Below is the coding -----------------------------------------------------------------------------------------------------------------------...