Maximum Nesting Depth of Two Valid Parentheses Strings - Problem

A string is a valid parentheses string (denoted VPS) if and only if it consists of ( and ) characters only, and:

  • It is the empty string, or
  • It can be written as AB (A concatenated with B), where A and B are VPS's, or
  • It can be written as (A), where A is a VPS.

We can similarly define the nesting depth depth(S) of any VPS S as follows:

  • depth("") = 0
  • depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's
  • depth("(" + A + ")") = 1 + depth(A), where A is a VPS.

For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), 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.

Input & Output

Example 1 — Balanced Distribution
$ Input: seq = "(()())"
Output: [0,1,1,1,1,0]
💡 Note: Split into A="()" (depth 1) and B="()()" (depth 1). The maximum depth is min(1,1) = 1, which is optimal.
Example 2 — Simple Case
$ Input: seq = "()(())"
Output: [0,0,1,1,1,1]
💡 Note: Split into A="()" (depth 1) and B="()()" (depth 1). Both have depth 1, giving maximum depth of 1.
Example 3 — Nested Structure
$ Input: seq = "(((())))"
Output: [0,1,0,1,1,0,1,0]
💡 Note: Alternating assignment distributes nested levels evenly: A="(())" (depth 2) and B="(())" (depth 2), max depth = 2.

Constraints

  • 1 ≤ seq.length ≤ 104
  • seq is a valid parentheses string

Visualization

Tap to expand
Maximum Nesting Depth of Two VPS INPUT seq = "(()())" ( ( ) ( ) ) 0 1 2 3 4 5 Nesting Depth: d=1 d=2 d=2 d=2 d=2 d=1 Original max depth = 2 Goal: Split to minimize max(depth_A, depth_B) ALGORITHM STEPS 1 Track Depth Count current nesting depth while traversing 2 Assign by Parity depth % 2 determines A (0) or B (1) 3 Process Each Char '(' : depth++, assign ')' : assign, depth-- 4 Build Result Store 0 or 1 for each character position Trace: i=0: '(' d=1 --> 1%2=1? No=0 i=1: '(' d=2 --> 2%2=0? No=1 i=2: ')' d=2 --> 2%2=0? No=1 i=3: '(' d=2 --> assign=0 i=4: ')' d=2 --> assign=1 i=5: ')' d=1 --> assign=0 FINAL RESULT Output Array: 0 1 1 0 1 0 Subsequences: A (0s): "( )" positions: 0, 3, 5 depth=1 B (1s): "( ) ( )" positions: 1, 2, 4 depth=1 OK - Optimal Split! max(1, 1) = 1 [0,1,1,0,1,0] Key Insight: By assigning parentheses based on depth parity (odd vs even), we evenly distribute the nesting levels between two subsequences. This guarantees both A and B have maximum depth of ceil(maxDepth/2), which is the optimal solution. Time: O(n), Space: O(n) for the result array. TutorialsPoint - Maximum Nesting Depth of Two Valid Parentheses Strings | Optimal Solution
Asked in
Google 25 Facebook 20 Amazon 15
42.0K Views
Medium Frequency
~15 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