1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-12 17:05:15 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/最长相邻不相等子序列 I [longest-unequal-adjacent-groups-subsequence-i].html
2025-09-29 14:43:44 +08:00

48 lines
2.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给定一个字符串数组&nbsp;<code>words</code>&nbsp;,和一个&nbsp;<strong>二进制</strong>&nbsp;数组&nbsp;<code>groups</code>&nbsp;,两个数组长度都是&nbsp;<code>n</code>&nbsp;</p>
<p>如果&nbsp;<code>words</code>&nbsp;的一个 <span data-keyword="subsequence-array">子序列</span> 是交替的,那么对于序列中的任意两个连续字符串,它们在&nbsp;<code>groups</code>&nbsp;中相同索引的对应元素是 <strong>不同</strong> 的(也就是说,不能有连续的 0 或 1</p>
<p>你需要从&nbsp;<code>words</code>&nbsp;中选出&nbsp;<strong>最长交替<span data-keyword="subsequence-array">子序列</span></strong></p>
<p>返回选出的子序列。如果有多个答案,返回 <strong>任意</strong> 一个。</p>
<p><b>注意:</b><code>words</code>&nbsp;中的元素是不同的&nbsp;</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>words = ["e","a","b"], groups = [0,0,1]
<b>输出:</b>["e","b"]
<strong>解释:</strong>一个可行的子序列是 [0,2] ,因为 groups[0] != groups[2] 。
所以一个可行的答案是 [words[0],words[2]] = ["e","b"] 。
另一个可行的子序列是 [1,2] ,因为 groups[1] != groups[2] 。
得到答案为 [words[1],words[2]] = ["a","b"] 。
这也是一个可行的答案。
符合题意的最长子序列的长度为 2 。</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>words = ["a","b","c","d"], groups = [1,0,1,1]
<b>输出:</b>["a","b","c"]
<b>解释:</b>一个可行的子序列为 [0,1,2] 因为 groups[0] != groups[1] 且 groups[1] != groups[2] 。
所以一个可行的答案是 [words[0],words[1],words[2]] = ["a","b","c"] 。
另一个可行的子序列为 [0,1,3] 因为 groups[0] != groups[1] 且 groups[1] != groups[3] 。
得到答案为 [words[0],words[1],words[3]] = ["a","b","d"] 。
这也是一个可行的答案。
符合题意的最长子序列的长度为 3 。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n == words.length == groups.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
<li><code>groups[i]</code>&nbsp;&nbsp;<code>0</code>&nbsp;&nbsp;<code>1</code></li>
<li><code>words</code>&nbsp;中的字符串 <strong>互不相同</strong>&nbsp;</li>
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
</ul>