A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:
AB (A concatenated with B), where A and B are VPS's, or(A), where A is a VPS.We can similarly define the nesting depth depth(S) of any VPS S as follows:
depth("") = 0depth(A + B) = max(depth(A), depth(B)), where A and B are VPS'sdepth("(" + A + ")") = 1 + depth(A), where A is a VPS.For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.
Given a VPS seq, split it into two disjoint subsequences A and B, such that A and B are VPS's (and A.length + B.length = seq.length).
Now choose any such A and B such that max(depth(A), depth(B)) is the minimum possible value.
Return an answer array (of length seq.length) that encodes such a choice of A and B: answer[i] = 0 if seq[i] is part of A, else answer[i] = 1. Note that even though multiple answers may exist, you may return any of them.
Example 1:
Input: seq = "(()())" Output: [0,1,1,1,1,0]
Example 2:
Input: seq = "()(())()" Output: [0,0,0,1,1,0,1,1]
Constraints:
1 <= seq.size <= 10000Problem Overview: You receive a valid parentheses string s. The task is to split it into two valid parentheses strings A and B such that the maximum nesting depth across both strings is minimized. Instead of constructing the strings directly, you return an array where each index indicates whether that parenthesis belongs to sequence 0 or 1.
The key observation: nesting depth increases when you see ( and decreases when you see ). If you distribute parentheses between two groups while tracking depth, you can ensure neither group accumulates excessive nesting.
Approach 1: Alternating Depth Approach (O(n) time, O(1) space)
This method tracks the current nesting depth while iterating through the string once. Each time you encounter (, increment the depth and assign the parenthesis to group depth % 2. When you see ), assign it using the current depth parity before decrementing. This effectively alternates nested layers between the two groups.
The insight: alternating by depth splits the nesting tree into two balanced halves. Deep levels automatically go to different groups, preventing one sequence from accumulating all the nesting. The algorithm performs a single pass through the string and only maintains an integer depth counter.
This approach is extremely concise and avoids auxiliary structures like stacks. If you already understand how nesting depth evolves in a valid parentheses string, this solution becomes almost mechanical.
Approach 2: Balanced Depth Assignment (O(n) time, O(1) space)
This approach also scans the string once but focuses on balancing how opening and closing parentheses are distributed between the two groups. Maintain a depth counter and decide the assignment based on whether the current level is even or odd. Opening parentheses increase the depth, and closing parentheses decrease it, while assignments ensure both sequences stay balanced.
The idea is similar to splitting a recursion tree into two interleaving layers. Each depth level alternates ownership between the two sequences, guaranteeing that the deepest nesting in the original string gets divided. This keeps the maximum depth of each resulting string close to originalDepth / 2.
Because the input is guaranteed to be valid, no explicit stack is required. A simple counter replaces the usual stack-based parsing commonly used for string parentheses problems.
Recommended for interviews: The alternating depth approach is the expected solution. It runs in O(n) time with O(1) extra space and demonstrates that you understand how nesting depth evolves in a parentheses string. Showing awareness of the depth distribution idea proves you can optimize beyond naive stack simulations.
An intuitive approach to minimize the maximum nesting depth of two valid parentheses sequences (A and B) is by alternating assignment of parenthesis indexes between the two sequences. The idea is to alternate between 0 and 1 whenever you encounter an opening parenthesis '('. Thus, you distribute the potential depth evenly, and consequently, the resulting nesting depth is minimized.
For a closing parenthesis ')', match it with the corresponding '(' in the same group it was started, which is straightforward given our alternating method.
This C program calculates the depth for each parenthesis in the string using a counter. When an opening parenthesis is encountered, the current depth is recorded modulo 2, alternating the assignment for each opening. For a closing parenthesis, it decrements the depth and then applies the same mod operation to decide on its group allocation.
Time Complexity: O(n) - We iterate through the string once.
Space Complexity: O(n) - We store the output in an answer array.
Another approach involves maintaining two counters to represent the potential depths of both sequences A and B. At every step, choose the sequence with a smaller current depth for the new '(' encountered and alternate in case of a tie. This ensures that the depths remain as balanced as possible, minimizing the maximum needed depth.
The approach involves tracking the current depth assigned to both sequences (A and B) and chooses the sequence with the lesser depth to assign the next opening bracket '('. When assigning the closing bracket ')', it ties it to the sequence it was originally paired with. This keeps depth differences minimized.
Time Complexity: O(n) - Single pass over the string.
Space Complexity: O(n) - Output storage for answer array.
We use a variable x to maintain the current balance of parentheses, which is the number of left parentheses minus the number of right parentheses.
We traverse the string seq, updating the value of x. If x is odd, we assign the current left parenthesis to A, otherwise we assign it to B.
The time complexity is O(n), where n is the length of the string seq. Ignoring the space consumption of the answer, the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Alternating Depth Approach | Time Complexity: O(n) - We iterate through the string once. |
| Balanced Depth Assignment | Time Complexity: O(n) - Single pass over the string. |
| Greedy | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Alternating Depth Approach | O(n) | O(1) | Best general solution. Minimal code and evenly splits nesting depth. |
| Balanced Depth Assignment | O(n) | O(1) | Useful when reasoning about distributing depth levels explicitly. |
1111. Maximum Nesting Depth of Two Valid Parentheses Strings (LeetCode Weekly Contest 144) • Kelvin Chandra • 5,201 views views
Watch 8 more video solutions →Practice Maximum Nesting Depth of Two Valid Parentheses Strings with our built-in code editor and test cases.
Practice on FleetCode