Search⌘ K
AI Features

Minimum Remove to Make Valid Parentheses

Explore how to apply stack data structures to solve the problem of removing the minimum number of invalid parentheses from a string. Learn to identify unmatched parentheses and implement a valid string solution that meets coding interview challenges.

Statement

Given a string, s, that may have matchedEach opening parenthesis, (, has a closing parenthesis, ), for it. and unmatchedThere is no corresponding closing parenthesis, ), for an opening parenthesis, (. parentheses, remove the minimum number of parentheses so that the resulting string represents a valid parenthesization. For example, the string “a(b)” represents a valid parenthesization while the string “a(b” doesn’t, since the opening parenthesis doesn’t have any corresponding closing parenthesis.

Constraints:

  • 11 \leq s.length 103\leq 10^3
  • s[i] is either an opening parenthesis (( , a closing parenthesiss )), or a lowercase English letter.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Minimum Remove to Make Valid Parentheses

1.

What is the output if the following string is given as input?

“(((abc)(to)((q)()(”

A.

“(((abc)(to)((q)()”

B.

“(abc)(to)((q)()”

C.

“(abc)(to)(q)()”

D.

“((abc)(to)((q)()”


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground:

JavaScript
usercode > main.js
export function minRemoveParentheses(s){
// Replace this placeholder return statement with your code
return "";
}
Minimum Remove to Make Valid Parentheses