Maximum Nesting Depth of Two Valid Parentheses Strings - Problem
Maximum Nesting Depth of Two Valid Parentheses Strings

Imagine you have a valid parentheses string (VPS) - a string containing only '(' and ')' characters that forms a perfectly balanced expression. Your task is to split this string into two subsequences such that both subsequences are also valid parentheses strings.

The nesting depth of a VPS is the maximum number of nested parentheses at any point. For example:
  • "()()" has depth 1
  • "(())" has depth 2
  • "((()))" has depth 3

Your goal is to split the original string into two subsequences A and B such that the maximum depth between A and B is minimized. In other words, you want to balance the nesting depth as evenly as possible between the two strings.

Return an array where answer[i] = 0 if character i belongs to subsequence A, and answer[i] = 1 if it belongs to subsequence B.

Input & Output

example_1.py β€” Basic Case
$ Input: seq = "(()())"
β€Ί Output: [0,1,1,1,0,0]
πŸ’‘ Note: Split into A="(())" (depth=2) and B="()" (depth=1). Max depth = max(2,1) = 2. Alternative splits might give higher max depths.
example_2.py β€” Deep Nesting
$ Input: seq = "(((())))"
β€Ί Output: [0,1,0,1,1,0,1,0]
πŸ’‘ Note: Alternating assignment creates A="(())" and B="(())", each with depth=2. This is optimal since any other split would create uneven depths.
example_3.py β€” Simple Case
$ Input: seq = "()()"
β€Ί Output: [0,0,1,1]
πŸ’‘ Note: Split into A="()" (depth=1) and B="()" (depth=1). Max depth = 1, which is optimal.

Constraints

  • 2 ≀ seq.length ≀ 105
  • seq consists only of '(' and ')' characters
  • seq is a valid parentheses string

Visualization

Tap to expand
Parentheses Team Balancing StrategyπŸ—οΈ Construction Analogy: Two Teams Building Nested StructuresTeam A (Red) - Even DepthsHandles levels: 0, 2, 4, 6...Example: ( ( ) ) from depth 0β†’1, 2β†’1Max concurrent nesting: ≀ ⌈total/2βŒ‰πŸ”¨ Balanced workloadTeam B (Green) - Odd DepthsHandles levels: 1, 3, 5, 7...Example: ( ) from depth 1β†’2, 1β†’0Max concurrent nesting: ≀ ⌊total/2βŒ‹πŸ”¨ Balanced workloadπŸ“Š Algorithm Walkthrough: "((()))"Step-by-Step Process:Position 0: '(' at depth=0 (even) β†’ Team A β†’ depth=1Position 1: '(' at depth=1 (odd) β†’ Team B β†’ depth=2Position 2: '(' at depth=2 (even) β†’ Team A β†’ depth=3Position 3: ')' at depth=2 (even) ← Team A ← depth=2Position 4: ')' at depth=1 (odd) ← Team B ← depth=1Position 5: ')' at depth=0 (even) ← Team A ← depth=0Result: A="(())" (depth=2), B="()" (depth=1) βœ“πŸ“ˆ Depth Chart0123Character Index🎯 Key InsightAlternating assignment by depth parity naturally balances the load, ensuring max depth difference ≀ 1
Understanding the Visualization
1
Track Current Level
Monitor the current nesting depth as we process each parenthesis
2
Alternate by Depth
Assign parentheses to Team 0 when depth is even, Team 1 when odd
3
Update Depth
Increment depth for '(' (starting new level), decrement for ')' (completing level)
4
Balanced Workload
Both teams end up with similar maximum nesting depths
Key Takeaway
🎯 Key Insight: By alternating team assignments based on nesting depth parity (evenβ†’Team A, oddβ†’Team B), we automatically achieve optimal load balancing with minimal computational overhead.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen