|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/delete-nodes-and-return-forest "Delete Nodes And Return Forest") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## 1111. Maximum Nesting Depth of Two Valid Parentheses Strings (Hard) |
| 13 | + |
| 14 | +<p>A string is a <em>valid parentheses string</em> (denoted VPS) if and only if it consists of <code>"("</code> and <code>")"</code> characters only, and:</p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li>It is the empty string, or</li> |
| 18 | + <li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are VPS's, or</li> |
| 19 | + <li>It can be written as <code>(A)</code>, where <code>A</code> is a VPS.</li> |
| 20 | +</ul> |
| 21 | + |
| 22 | +<p>We can similarly define the <em>nesting depth</em> <code>depth(S)</code> of any VPS <code>S</code> as follows:</p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li><code>depth("") = 0</code></li> |
| 26 | + <li><code>depth(A + B) = max(depth(A), depth(B))</code>, where <code>A</code> and <code>B</code> are VPS's</li> |
| 27 | + <li><code>depth("(" + A + ")") = 1 + depth(A)</code>, where <code>A</code> is a VPS.</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p>For example, <code>""</code>, <code>"()()"</code>, and <code>"()(()())"</code> are VPS's (with nesting depths 0, 1, and 2), and <code>")("</code> and <code>"(()"</code> are not VPS's.</p> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | + |
| 34 | +<p>Given a VPS <font face="monospace">seq</font>, split it into two disjoint subsequences <code>A</code> and <code>B</code>, such that <code>A</code> and <code>B</code> are VPS's (and <code>A.length + B.length = seq.length</code>).</p> |
| 35 | + |
| 36 | +<p>Now choose <strong>any</strong> such <code>A</code> and <code>B</code> such that <code>max(depth(A), depth(B))</code> is the minimum possible value.</p> |
| 37 | + |
| 38 | +<p>Return an <code>answer</code> array (of length <code>seq.length</code>) that encodes such a choice of <code>A</code> and <code>B</code>: <code>answer[i] = 0</code> if <code>seq[i]</code> is part of <code>A</code>, else <code>answer[i] = 1</code>. Note that even though multiple answers may exist, you may return any of them.</p> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Example 1:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> seq = "(()())" |
| 45 | +<strong>Output:</strong> [0,1,1,1,1,0] |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p><strong>Example 2:</strong></p> |
| 49 | + |
| 50 | +<pre> |
| 51 | +<strong>Input:</strong> seq = "()(())()" |
| 52 | +<strong>Output:</strong> [0,0,0,1,1,0,1,1] |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p> </p> |
| 56 | +<p><strong>Constraints:</strong></p> |
| 57 | + |
| 58 | +<ul> |
| 59 | + <li><code>1 <= text.size <= 10000</code></li> |
| 60 | +</ul> |
0 commit comments