Dsa
Prefix Sum
Medium
Balanced Parentheses with Prefix Sum
You are tasked with verifying if a string consisting of parentheses is balanced using a prefix sum approach. A string is considered balanced if every opening parenthesis has a corresponding closing parenthesis.
Implement a function that checks if the given string of parentheses is balanced. A prefix sum can be used to maintain a count of opening and closing parentheses.
Input format:
- A single line containing the string of parentheses, consisting of characters '(' and ')'.
Output format:
- Print 'True' if the parentheses are balanced, otherwise print 'False'.
Example:
Input:
(())()
Output:
True
Key concepts
prefix_sumsyntax_validationstack
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