Minimum Remove to Make Valid Parentheses
Problem Statement
You are given a string s of lowercase letters and the brackets ( and ).
Delete the fewest brackets possible so that the brackets in the string become valid, and return the resulting string. Letters are never deleted.
Valid means: every ( has a ) after it that closes it, and every ) closes some ( before it. A string with no brackets is valid. If there are several correct answers, return any one of them.
A string s of lowercase letters, ( and ).
A valid string made by deleting as few brackets as possible from s.
The last ) has nothing left to close, so it goes. "lee(t(co)de)" or "lee(t(c)ode)" would also be accepted.
The first ) comes before any (, so it can never be matched.
No bracket can be matched, so all four are deleted.
- • 1 ≤ s.length ≤ 10^5
- • s[i] is '(', ')' or a lowercase English letter
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Scan with a counter, delete the first bad bracket found, rescan
Left→right drop extra ')', then right→left drop extra '('
Push indices of '('; unmatched ')' marked now, leftover '(' marked at the end
Scan with a counter of open brackets. The first ) that arrives while the counter is 0 is surely bad: delete it and scan again from the start. If a scan finds no bad ) but the counter ends above 0, some ( were never closed; the last ( is surely one of them, so delete it and scan again. Stop when the string is valid.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Stack translate into production architectures and low-level memory mechanics.