Longest Unequal Adjacent Groups Subsequence I
You are given a string array words and a binary array groups, both of length n.
A subsequence of words is alternating if, for any two consecutive strings in the sequence, their corresponding elements at the same indices in groups are different. In other words, there cannot be consecutive 0 or 1 values among the selected indices.
Your task is to select the longest alternating subsequence from words.
Return the selected subsequence. If there are multiple answers, return any of them.
Note: The elements in words are distinct.
Example 1
Input
words = ["e","a","b"], groups = [0,0,1]Output
["e","b"]A longest alternating subsequence is
["e", "b"] because groups[0] != groups[2]; ["a", "b"] is also valid, and the maximum length is 2.Example 2
Input
words = ["a","b","c","d"], groups = [1,0,1,1]Output
["a","b","c"]A longest alternating subsequence is
["a", "b", "c"] because groups[0] != groups[1] and groups[1] != groups[2]; ["a", "b", "d"] is also valid, and the maximum length is 3.Constraints
- 1 <= n == words.length == groups.length <= 100
- 1 <= words[i].length <= 10
- groups[i] is either 0 or 1.
- words consists of distinct strings.
- words[i] consists of lowercase English letters.