Dsa
Stack
Medium
Balanced Parentheses with History
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 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.
Also, keep track of and print all pairs of matched brackets as you validate the string.
Input Format:
A single line containing the string of brackets.
Output Format:
If the string is valid, print 'Valid' followed by the pairs of matched brackets. If it is not valid, print 'Invalid'.
Example:
Input:
{[()()]}
Output:
Valid
Matched Pairs: (()), []
Key concepts
stackparenthesesvalidation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now