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.

Find Pairs with Given Sum in a Sorted Array – Java Code

Find pairs with given sum in a sorted array.

Given an array A of size N. Write a code to find all pairs in the array that sum to a number equal to K. If no such pair exists then output will be 1.

NOTE – The array elements are distinct and in a sorted order.

For example –

Input :

arr[] = {1, 2, 3, 4, 5, 6, 7};
sum = 9

Output:

Pairs with given sum 9 is

Pair (2 , 7 )
Pair (3 , 6 )
Pair (4 , 5 )