parentheses
string-validation
algorithms
coding-interview
data-structures

Minimum add to make parentheses string consisting of '', '', '', '', '', '' valid

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In programming and mathematical contexts, parentheses are often used to define precedence or grouping of expressions. Ensuring the correctness of these expressions often requires validating that every open parenthesis has a matching close parenthesis. In this article, we will explore how to make a string containing the characters '{', '}', '[', ']', '(', ')' valid by adding the minimum number of parentheses.

Understanding Parentheses Validity

A string of parentheses is considered valid if:

  1. Every opening parenthesis has a corresponding closing parenthesis.
  2. Parentheses must close in the correct order.

For example, the string {[()]} is valid because each type of parenthesis is correctly opened and closed in the right order. Meanwhile, the string {[}] is invalid due to an incorrect closing order.

Problem Statement

Given a string containing {, }, [, ], (, and ), our goal is to determine the minimum number of parentheses required to be added to make the string valid.

Example Strings and Their Validity

  • "{}[]()" - Valid
  • "{[}]" - Invalid
  • "[({})]" - Valid
  • "({[})" - Invalid

Approach to the Solution

To solve this problem, one common approach is using a stack data structure. The stack helps in keeping track of unmatched open parentheses while scanning through the string:

  1. Initialize an empty stack.
  2. Iterate through each character in the string:
    • If the character is an opening parenthesis ({, [, or (), push it onto the stack.
    • If the character is a closing parenthesis (}, ], or )), check if the stack is not empty and the top of the stack is a matching opening parenthesis. If so, pop the stack. Otherwise, increment a counter for unmatched closing parentheses.
  3. Finally, after processing all characters, the stack will contain unmatched opening parentheses and the counter will provide unmatched closing parentheses.

The sum of unmatched opening and closing parentheses gives the minimum number of parentheses to add to make the string valid.

Illustration with Example

Consider the string "{[}]":

  • Step 1: { is an opening brace, push to stack. Stack: ["{"]
  • Step 2: [ is an opening bracket, push to stack. Stack: ["{", "["]
  • Step 3: } is a closing brace, does not match with [. Increment counter. Unmatched closes: 1
  • Step 4: ] is a closing bracket, matches opening bracket [ on top of stack. Pop stack. Stack: ["{"]
  • End: Unmatched on stack: 1. Total unmatched: 1 unmatched closing + 1 unmatched opening = 2.

We need to add a minimum of 2 parentheses to make the entire string valid.

Complexity Analysis

The time complexity of this approach is O(n)O(n), where nn is the length of the string, because each character is processed exactly once. The space complexity is also O(n)O(n) in the worst case, where all characters are opening parentheses and thus stored in the stack.

Summary

ConceptExplanation
Valid Parentheses StringContains matching and correctly ordered parentheses.
Approach UsedUtilize a stack to track unmatched opening parentheses.
Key OperationsPush for opening, pop for matched closing, count for unmatched closing.
ComplexityTime: O(n)O(n), Space: O(n)O(n)
Minimum AdditionsUnmatched opening from stack + unmatched closing count

In conclusion, by leveraging the stack data structure, we can effectively and efficiently determine how many parentheses need to be added to make any given string of '{', '}', '[', ']', '(', ')' valid. This solution provides a robust way of verifying and fixing parentheses strings in computational tasks.


Course illustration
Course illustration

All Rights Reserved.