Compute Minimum Parentheses Additions
ByteDanceData Scientist · Technical ScreenLast updated
This question evaluates proficiency in string processing and algorithmic reasoning for balancing parentheses, testing understanding of invariants and edge-case handling.
Given a string s consisting only of the characters '(' and ')', compute the minimum number of parentheses that must be inserted anywhere in the string so that the resulting string is a valid parentheses string.
A valid parentheses string is one where every opening parenthesis has a matching closing parenthesis, every closing parenthesis matches a previous unmatched opening parenthesis, and the pairs are properly nested.
Return a single integer: the minimum number of insertions required.
Example:
Input: s = "()))(("
Output: 4
Explain your approach and analyze its time and space complexity.
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.