Minimum Remove to Make Valid Parentheses
Updated
Problem statement
You are given a string s made of the characters '(', ')' and lowercase English letters.
Remove as few parentheses as possible so that the parentheses left in the string are valid. Letters are never removed, and the remaining characters keep their order. The parentheses are valid when every '(' can be paired with a later ')' and every ')' with an earlier '(', each character used in only one pair, and no two pairs crossing. A string with no parentheses at all is valid.
Often several strings remove the same smallest number of parentheses. Return the one made by this rule: read s from left to right and pair each ')' with the nearest '(' before it that has no partner yet. Remove every ')' that finds no partner, and at the end remove every '(' that never got one.
Function
minRemoveToMakeValid(s: String) → StringExamples
Example 1
s = "a)b(c)d"return = "ab(c)d"The ) right after a has no ( before it, so it is removed. The remaining pair around c is fine.
Example 2
s = "(a(b)"return = "a(b)"The ) pairs with the nearest unpaired ( before it, the one just before b. The first ( is left without a partner and is removed. Removing the other ( instead would give "(ab)", which also removes one character, but the pairing rule picks "a(b)".
Example 3
s = "))(("return = ""Neither ) has a ( before it, and neither ( has a ) after it. All four are removed, so the answer is the empty string.
Example 4
s = "x(y)z)("return = "x(y)z"The pair around y stays. The ) after z has nothing left to pair with, and the final ( has no ) after it. Both are removed.
Example 5
s = "(ab)(c)"return = "(ab)(c)"Every parenthesis already has a partner, so nothing is removed.
Constraints
1 <= s.length <= 105s[i]is'(',')'or a lowercase English letter.
More WhatNot Onsite Interview questions
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Detect Blocked Chat PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2024
- Merge Two Sorted ListsONSITE INTERVIEW · Seen Jul 2024
- Number of Submatrices That Sum to TargetONSITE INTERVIEW · Seen Jul 2024
- Parse and Store Log RecordsONSITE INTERVIEW · Seen Oct 2023
- Room Occupancy at Query TimesONSITE INTERVIEW · Seen Oct 2023
- K Smallest Values in a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2022
- Average Purchase PriceONSITE INTERVIEW · Seen Sep 2022
Similar questions at other companies
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Number Words to Digits in Any LanguageMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Inorder Tree String EquivalenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions