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). The subsequences may not necessarily be contiguous.
For example, for the sequence 123456789, one possible split is:
A = {1, 3, 5, 7, 9},
B = {2, 4, 6, 8}.
This corresponds to the output [0, 1, 0, 1, 0, 1, 0, 1, 0] where 0 indicates membership in A and 1 indicates membership in B.
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 <= 10000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to distribute parentheses of a valid sequence between two groups such that the maximum depth between the two groups is minimized. We can achieve this by distributing alternating nesting levels to different groups. Using the parity of the current nesting level naturally splits the depth as evenly as possible.
Here's how the algorithm would work step-by-step:
def max_depth_after_split(seq: str) -> list[int]:
result = []
current_depth = 0
# Iterate through each character in the given sequence
for char in seq:
if char == "(":
# Opening parentheses take the parity of the current depth
result.append(current_depth % 2)
current_depth += 1
else:
current_depth -= 1
# Closing parentheses take the parity of the decremented depth
result.append(current_depth % 2)
return result
To divide a parenthesized string into two separate valid groups while keeping their nesting depths as small as possible, the secret is to share the load evenly. By looking at how deep each parenthesis is, we can simply alternate assigning them to our two groups based on whether their depth is even or odd.
Here's how the algorithm would work step-by-step:
def max_depth_after_split(parentheses_string):
current_depth_level = 0
# Alternating group assignment based on parity keeps the maximum nesting depth minimized.
answer_group_mapping = []
for character in parentheses_string:
if character == '(':
# Parity checks distribute nesting load evenly across both groups to limit peak depth.
answer_group_mapping.append(current_depth_level % 2)
current_depth_level += 1
else:
current_depth_level -= 1
answer_group_mapping.append(current_depth_level % 2)
# Returning the mapped assignments provides the exact two valid split groups requested.
return answer_group_mapping
| Case | How to Handle |
|---|---|
| Minimum length valid parentheses string like "()" | The algorithm correctly assigns depth-based alternating indices yielding a maximum depth of zero or one. |
| Deeply nested string like "(((())))" | By alternating assignments based on the current nesting depth modulo two, the maximum depth is successfully split and minimized. |
| Already alternating valid string like "()()()()" | The parity tracking assigns adjacent matching pairs to the same group without increasing the shared maximum depth. |
| Maximum length constraints up to the problem limits | A single linear pass ensures optimal O(n) time complexity and O(n) space complexity to scale efficiently. |
| String with multiple valid partitioning outcomes | Any valid assignment that achieves the theoretical minimum maximum depth of nesting is accepted by the grading criteria. |
| Unbalanced or invalid parentheses strings | The problem guarantees the input seq is a valid parentheses string so invalid inputs do not need to be handled. |