Minimum add to make parentheses string consisting of '', '', '', '', '', '' valid
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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:
- Every opening parenthesis has a corresponding closing parenthesis.
- 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:
- Initialize an empty stack.
- 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.
- 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:1unmatched closing +1unmatched 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 , where is the length of the string, because each character is processed exactly once. The space complexity is also in the worst case, where all characters are opening parentheses and thus stored in the stack.
Summary
| Concept | Explanation |
| Valid Parentheses String | Contains matching and correctly ordered parentheses. |
| Approach Used | Utilize a stack to track unmatched opening parentheses. |
| Key Operations | Push for opening, pop for matched closing, count for unmatched closing. |
| Complexity | Time: , Space: |
| Minimum Additions | Unmatched 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.
Related reading
- Minimum area quadrilateral algorithm
- minimum connected subgraph containing a given set of nodes
- Minimum cost factoring in abelian groups
- Minimum Cost Flow - network optimization in R
- Minimum no of changes required to make array strictly increasing
- Minimum number of swaps needed to change Array 1 to Array 2?
- minimum difference between sum of two subsets
- Minimum exact cover of grid with squares; extra cuts

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.