Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Valid Parentheses

Easy
0/40
Average time to solve is 10m
Contributed by
336 upvotes
Asked in companies

Problem statement

You're given a string 'S' consisting of "{", "}", "(", ")", "[" and "]" .

Return true if the given string 'S' is balanced, else return false.

For example:
``````'S' = "{}()".

There is always an opening brace before a closing brace i.e. '{' before '}', '(' before ').
So the 'S' is Balanced.
``````
Detailed explanation ( Input/output format, Notes, Images )
Sample Input 1 :
``````[()]{}{[()()]()}
``````
Sample Output 1 :
``````Balanced
``````
Explanation Of the Sample Input 1 :
``````There is always an opening brace before a closing brace i.e. '{' before '}', '(' before '), '[' before ']'.
So the 'S' is Balanced.
``````
Sample Input 2 :
``````[[}[
``````
Sample Output 2 :
``````Not Balanced
``````
Constraints:
``````1 <= 'N' <= 10^5

Where 'N' is the length of the input string 'S'.
Time Limit: 1 sec
``````
Console