Check Balanced Parentheses in an Expression

Write a java code to check balanced parentheses in an expression using stack.

Given an expression containing characters ‘{‘,’}’,'(‘,’)’,'[‘,’]’. We have to write a code to check whether an input string has valid parentheses.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

Example –

i) {[]}) – Invalid

ii) {()}[] – Valid

Java Program to Reverse a String using Stack

Write a java program to reverse a string using stack data structure. Given an input string, We have to write a java code to reverse a string using stack.

In my previous tutorial, I have explained java program to reverse a string using recursion.

Before solving this problem, Let’s understand what is a stack data structure.