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.
words = ["e","a","b"], groups = [0,0,1]["e","b"]["e", "b"] because groups[0] != groups[2]; ["a", "b"] is also valid, and the maximum length is 2.words = ["a","b","c","d"], groups = [1,0,1,1]["a","b","c"]["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.
Asked at 1 companies
Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.