|
| 1 | +<p>You are given a string array <code>words</code> and a <strong>binary</strong> array <code>groups</code> both of length <code>n</code>, where <code>words[i]</code> is associated with <code>groups[i]</code>.</p> |
| 2 | + |
| 3 | +<p>Your task is to select the <strong>longest alternating</strong> <span data-keyword="subsequence-array">subsequence</span> from <code>words</code>. A subsequence of <code>words</code> is alternating if for any two consecutive strings in the sequence, their corresponding elements in the binary array <code>groups</code> differ. Essentially, you are to choose strings such that adjacent elements have non-matching corresponding bits in the <code>groups</code> array.</p> |
| 4 | + |
| 5 | +<p>Formally, you need to find the longest subsequence of an array of indices <code>[0, 1, ..., n - 1]</code> denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k-1</sub>]</code>, such that <code>groups[i<sub>j</sub>] != groups[i<sub>j+1</sub>]</code> for each <code>0 <= j < k - 1</code> and then find the words corresponding to these indices.</p> |
| 6 | + |
| 7 | +<p>Return <em>the selected subsequence. If there are multiple answers, return <strong>any</strong> of them.</em></p> |
| 8 | + |
| 9 | +<p><strong>Note:</strong> The elements in <code>words</code> are distinct.</p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<div class="example-block" style=" |
| 15 | + border-color: var(--border-tertiary); |
| 16 | + border-left-width: 2px; |
| 17 | + color: var(--text-secondary); |
| 18 | + font-size: .875rem; |
| 19 | + margin-bottom: 1rem; |
| 20 | + margin-top: 1rem; |
| 21 | + overflow: visible; |
| 22 | + padding-left: 1rem; |
| 23 | +"> |
| 24 | +<p><strong>Input:</strong> <span class="example-io" style=" |
| 25 | + font-family: Menlo,sans-serif; |
| 26 | + font-size: 0.85rem; |
| 27 | +">words = ["e","a","b"], groups = [0,0,1]</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io" style=" |
| 30 | + font-family: Menlo,sans-serif; |
| 31 | + font-size: 0.85rem; |
| 32 | +">["e","b"]</span></p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong> A subsequence that can be selected is <code>["e","b"]</code> because <code>groups[0] != groups[2]</code>. Another subsequence that can be selected is <code>["a","b"]</code> because <code>groups[1] != groups[2]</code>. It can be demonstrated that the length of the longest subsequence of indices that satisfies the condition is <code>2</code>.</p> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p><strong class="example">Example 2:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block" style=" |
| 40 | + border-color: var(--border-tertiary); |
| 41 | + border-left-width: 2px; |
| 42 | + color: var(--text-secondary); |
| 43 | + font-size: .875rem; |
| 44 | + margin-bottom: 1rem; |
| 45 | + margin-top: 1rem; |
| 46 | + overflow: visible; |
| 47 | + padding-left: 1rem; |
| 48 | +"> |
| 49 | +<p><strong>Input:</strong> <span class="example-io" style=" |
| 50 | + font-family: Menlo,sans-serif; |
| 51 | + font-size: 0.85rem; |
| 52 | +">words = ["a","b","c","d"], groups = [1,0,1,1]</span></p> |
| 53 | + |
| 54 | +<p><strong>Output:</strong> <span class="example-io" style=" |
| 55 | + font-family: Menlo,sans-serif; |
| 56 | + font-size: 0.85rem; |
| 57 | +">["a","b","c"]</span></p> |
| 58 | + |
| 59 | +<p><strong>Explanation:</strong> A subsequence that can be selected is <code>["a","b","c"]</code> because <code>groups[0] != groups[1]</code> and <code>groups[1] != groups[2]</code>. Another subsequence that can be selected is <code>["a","b","d"]</code> because <code>groups[0] != groups[1]</code> and <code>groups[1] != groups[3]</code>. It can be shown that the length of the longest subsequence of indices that satisfies the condition is <code>3</code>.</p> |
| 60 | +</div> |
| 61 | + |
| 62 | +<p> </p> |
| 63 | +<p><strong>Constraints:</strong></p> |
| 64 | + |
| 65 | +<ul> |
| 66 | + <li><code>1 <= n == words.length == groups.length <= 100</code></li> |
| 67 | + <li><code>1 <= words[i].length <= 10</code></li> |
| 68 | + <li><code>groups[i]</code> is either <code>0</code> or <code>1.</code></li> |
| 69 | + <li><code>words</code> consists of <strong>distinct</strong> strings.</li> |
| 70 | + <li><code>words[i]</code> consists of lowercase English letters.</li> |
| 71 | +</ul> |
0 commit comments