Remove minimum parentheses to balance string
MetaSoftware Engineer · Technical ScreenLast updated
This question evaluates string-processing and algorithm-design skills within the Coding & Algorithms domain, focusing on balancing parentheses, correctness proofs for minimal edits, and time/space complexity analysis.
Given a string s containing lowercase letters and the characters '(' and ')', remove the minimum number of parentheses so that the resulting string is valid (balanced and properly ordered). Return any one valid result while preserving the relative order of the remaining characters. Target O(n) time and O(n) or O(
- extra space. Describe the algorithm, argue minimality, and analyze complexity. As a follow-up, discuss how to output all possible valid results if multiple exist.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.