2900. Longest Unequal Adjacent Groups Subsequence I Easy
1/**
2 * [2900] Longest Unequal Adjacent Groups Subsequence I
3 *
4 * You are given a string array words and a binary array groups both of length n, where words[i] is associated with groups[i].
5 * Your task is to select the longest alternating <span data-keyword="subsequence-array">subsequence</span> from words. A subsequence of words is alternating if for any two consecutive strings in the sequence, their corresponding elements in the binary array groups differ. Essentially, you are to choose strings such that adjacent elements have non-matching corresponding bits in the groups array.
6 * Formally, you need to find the longest subsequence of an array of indices [0, 1, ..., n - 1] denoted as [i0, i1, ..., ik-1], such that groups[ij] != groups[ij+1] for each 0 <= j < k - 1 and then find the words corresponding to these indices.
7 * Return the selected subsequence. If there are multiple answers, return any of them.
8 * Note: The elements in words are distinct.
9 *
10 * <strong class="example">Example 1:
11 * <div class="example-block" style="
12 * border-color: var(--border-tertiary);
13 * border-left-width: 2px;
14 * color: var(--text-secondary);
15 * font-size: .875rem;
16 * margin-bottom: 1rem;
17 * margin-top: 1rem;
18 * overflow: visible;
19 * padding-left: 1rem;
20 * ">
21 * Input: <span class="example-io" style="
22 * font-family: Menlo,sans-serif;
23 * font-size: 0.85rem;
24 * ">words = ["e","a","b"], groups = [0,0,1]</span>
25 * Output: <span class="example-io" style="
26 * font-family: Menlo,sans-serif;
27 * font-size: 0.85rem;
28 * ">["e","b"]</span>
29 * Explanation: A subsequence that can be selected is ["e","b"] because groups[0] != groups[2]. Another subsequence that can be selected is ["a","b"] because groups[1] != groups[2]. It can be demonstrated that the length of the longest subsequence of indices that satisfies the condition is 2.
30 * </div>
31 * <strong class="example">Example 2:
32 * <div class="example-block" style="
33 * border-color: var(--border-tertiary);
34 * border-left-width: 2px;
35 * color: var(--text-secondary);
36 * font-size: .875rem;
37 * margin-bottom: 1rem;
38 * margin-top: 1rem;
39 * overflow: visible;
40 * padding-left: 1rem;
41 * ">
42 * Input: <span class="example-io" style="
43 * font-family: Menlo,sans-serif;
44 * font-size: 0.85rem;
45 * ">words = ["a","b","c","d"], groups = [1,0,1,1]</span>
46 * Output: <span class="example-io" style="
47 * font-family: Menlo,sans-serif;
48 * font-size: 0.85rem;
49 * ">["a","b","c"]</span>
50 * Explanation: A subsequence that can be selected is ["a","b","c"] because groups[0] != groups[1] and groups[1] != groups[2]. Another subsequence that can be selected is ["a","b","d"] because groups[0] != groups[1] and groups[1] != groups[3]. It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
51 * </div>
52 *
53 * Constraints:
54 *
55 * 1 <= n == words.length == groups.length <= 100
56 * 1 <= words[i].length <= 10
57 * groups[i] is either 0 or 1.
58 * words consists of distinct strings.
59 * words[i] consists of lowercase English letters.
60 *
61 */
62pub struct Solution {}
63
64// problem: https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i/
65// discuss: https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i/discuss/?currentPage=1&orderBy=most_votes&query=
66
67// submission codes start here
68
69impl Solution {
70 pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
71 vec![]
72 }
73}
74
75// submission codes end
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_2900() {
83 }
84}
85
Back
© 2025 bowen.ge All Rights Reserved.