Advertisement

Minimum Insertions to Balance a Parentheses String - LeetCode 1541 Solution

Minimum Insertions to Balance a Parentheses String - Complete Solution Guide

Minimum Insertions to Balance a Parentheses String is LeetCode problem 1541, 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

Given a parentheses string s containing only the characters '(' and ')' . A parentheses string is balanced if: Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))' . Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))' . In other words, we treat '(' as an opening parenthesis and '))' as a closing parenthesis. For example, "())" , "())(())))" and "(())())))" are balanced, ")()" , "()))" and "(()))" are not balanced. You

Detailed Explanation

The problem asks us to find the minimum number of insertions (of '(' or ')') needed to balance a parentheses string `s` consisting only of '(' and ')'. A string is considered balanced if every '(' has two consecutive ')' following it. In other words, '(' acts as an opening parenthesis and '))' acts as a closing parenthesis. The insertions can be made at any position in the string.

Solution Approach

The solution iterates through the input string `s`. It maintains a variable `right_needed` that represents the number of ')' needed to balance the previously encountered '('. When an opening parenthesis '(' is encountered, we check if `right_needed` is odd, meaning there is one ')' short from previous parenthesis. If so, we increment `insertions` and decrease `right_needed`. Then, we increment `right_needed` by 2 because each '(' needs two ')'. When a closing parenthesis ')' is encountered, we decrease `right_needed` by 1. If `right_needed` becomes negative, it means we have more ')' than expected, so we need to insert a '(' to balance, which means we increment `insertions` and set `right_needed` to 1. Finally, after iterating through the whole string, we add the remaining `right_needed` to `insertions`, since these are the number of ')' needed at the end.

Step-by-Step Algorithm

  1. Step 1: Initialize `insertions` and `right_needed` to 0. `insertions` will store the final answer, and `right_needed` will track how many closing parenthesis are needed at any given point.
  2. Step 2: Iterate through the input string `s` character by character.
  3. Step 3: If the current character is '(', check if `right_needed` is odd. If it is, increment `insertions` by 1 and decrement `right_needed` by 1 (simulating insertion of a ')'). Then, increment `right_needed` by 2, because a '(' needs two ')'.
  4. Step 4: If the current character is ')', decrement `right_needed` by 1.
  5. Step 5: If `right_needed` becomes negative, it means we need to insert a '(' to balance. Increment `insertions` by 1 and reset `right_needed` to 1, as we have just 'used' one ')' to pair with the inserted '('.
  6. Step 6: After the loop finishes, add the final `right_needed` to `insertions`. This handles the case where there are remaining '(' that need to be closed with '))' at the end of the string.
  7. Step 7: Return the final value of `insertions`.

Key Insights

  • Insight 1: The core idea is to iterate through the string and keep track of the number of ')' needed to balance the encountered '('.
  • Insight 2: Instead of using a stack, we can use a counter to represent the number of ')' required. This avoids the O(n) space complexity associated with stacks in some cases.
  • Insight 3: We need to handle cases where we encounter a ')' but no '(' is present, meaning we need to add a '(' before. Also, if we end up with remaining '(' without enough '))', we need to add the required '))' at the end.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String, Stack, Greedy.

Companies

Asked at: J.P. Morgan.