Input 1 : "{{{}}" Output : false; Input 2 : "{{}}" Output : true
push
and pop
operations to identify if the braces are balanced.
1. Create a Stack
.
2. Convert the input to a character array
.
3. Traverse through the characters, if the character is {
, push into the stack
.
4. Else if the character is }
pop
out a character from the stack
.
5. At the end of the loop
, if the stack
is empty
, return true
.
6. Else return false
.
public static boolean isBalanced(String input){ }
C
Java
Python