Update appNew update is available. Click here to update.

Valid Parentheses

Contributed by
Amandeep Kaur
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 10 mins
Success Rate 80 %
Share
140 upvotes

Problem Statement

You're given string ‘STR’ consisting solely of “{“, “}”, “(“, “)”, “[“ and “]” . Determine whether the parentheses are balanced.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 10
1 <= N <= 10^5

Where N is the length of the input string or expression.

Time Limit: 1 sec
Sample Input 1 :
2
[()]{}{[()()]()}
[(])
Sample Output 1 :
Balanced
Not Balanced
Explanation Of the Sample Input 1 :
In TestCase 1 there is always an opening brace before a closing brace i.e ‘{‘ before ‘}’, ‘(‘ before ‘)’, ‘[‘ before ‘]’.

In TestCase 2 there is closing brace for ‘[‘ i.e. ‘]’ before closing brace for ‘(‘ i.e. ‘)’. The balanced sequence should be ‘[()]’.
Sample Input 2 :
2
[[}[
[]{}()
Sample Output 2 :
Not Balanced
Balanced
Explanation Of the Sample Input 2 :
In TestCase 1 there is no opening brace before a closing brace i.e no ‘{‘ for ‘}’.

In TestCase 2 there is exactly one closing for each opening braces and each closing brace is after their corresponding opening brace.
Reset Code
Full screen
Auto
copy-code
Console