Advertisement

Minimum Add to Make Parentheses Valid - LeetCode 921 Solution

Minimum Add to Make Parentheses Valid - Complete Solution Guide

Minimum Add to Make Parentheses Valid is LeetCode problem 921, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A parentheses string is valid if and only if: It is the empty string, It can be written as AB ( A concatenated with B ), where A and B are valid strings, or It can be written as (A) , where A is a valid string. You are given a parentheses string s . In one move, you can insert a parenthesis at any position of the string. For example, if s = "()))" , you can insert an opening parenthesis to be "( ( )))" or a closing parenthesis to be "()) ) )" . Return the minimum number of moves required to make

Detailed Explanation

The problem asks us to determine the minimum number of parentheses (either '(' or ')') we need to add to a given string `s` of parentheses so that the resulting string is a valid parentheses string. A valid parentheses string is defined as either empty, formed by concatenating two valid parentheses strings, or enclosed in parentheses like `(A)` where `A` is another valid parentheses string. In simpler terms, we need to balance the parentheses in the input string by adding the missing opening or closing parentheses.

Solution Approach

The solution uses a greedy approach. We iterate through the string, character by character. If we encounter an opening parenthesis '(', we increment a counter `close_needed`, indicating that we need a corresponding closing parenthesis. If we encounter a closing parenthesis ')', we check if there's an open parenthesis waiting to be closed (i.e., `close_needed > 0`). If so, we decrement `close_needed`. Otherwise, it means this closing parenthesis has no corresponding opening parenthesis, so we increment `open_needed`, indicating that we need to add an opening parenthesis to balance it. Finally, we return the sum of `open_needed` and `close_needed`, which represents the total number of parentheses we need to add.

Step-by-Step Algorithm

  1. Step 1: Initialize two integer variables, `open_needed` and `close_needed`, to 0. These will keep track of the number of missing opening and closing parentheses, respectively.
  2. Step 2: Iterate through the input string `s` character by character.
  3. Step 3: If the current character is '(', increment `close_needed`. This means we need one more closing parenthesis later.
  4. Step 4: If the current character is ')', check if `close_needed` is greater than 0. If it is, decrement `close_needed` because this closing parenthesis has found a matching opening parenthesis. If `close_needed` is 0, it means we have an unmatched closing parenthesis, so increment `open_needed`. This signifies the need for an opening parenthesis.
  5. Step 5: After iterating through the entire string, return the sum of `open_needed` and `close_needed`. This sum represents the minimum number of parentheses that need to be added to make the string valid.

Key Insights

  • Insight 1: We can determine the number of extra '(' or ')' needed by iterating through the string and keeping track of the balance between open and close parentheses.
  • Insight 2: We don't need a stack data structure to solve this problem. Instead, we can maintain counters for the number of open parentheses that need to be closed and the number of close parentheses that need to be opened.
  • Insight 3: The problem can be solved in a single pass through the input string using a greedy approach, making it efficient.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String, Stack, Greedy.

Companies

Asked at: Nvidia, Siemens, Walmart Labs.