mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
48 lines
2.4 KiB
HTML
48 lines
2.4 KiB
HTML
<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>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li>It is the empty string, or</li>
|
|
|
|
<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>
|
|
|
|
<li>It can be written as <code>(A)</code>, where <code>A</code> is a VPS.</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>We can similarly define the <em>nesting depth</em> <code>depth(S)</code> of any VPS <code>S</code> as follows:</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>depth("") = 0</code></li>
|
|
|
|
<li><code>depth(A + B) = max(depth(A), depth(B))</code>, where <code>A</code> and <code>B</code> are VPS's</li>
|
|
|
|
<li><code>depth("(" + A + ")") = 1 + depth(A)</code>, where <code>A</code> is a VPS.</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<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>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
|
|
|
|
<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>
|
|
|
|
|
|
|
|
<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>
|
|
|
|
|
|
|
|
<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>
|
|
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> seq = "(()())"
|
|
<strong>Output:</strong> [0,1,1,1,1,0]
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> seq = "()(())()"
|
|
<strong>Output:</strong> [0,0,0,1,1,0,1,1]
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= seq.size <= 10000</code></li>
|
|
</ul>
|