You are given a string array words and a binary array groups both of length n, where words[i] is associated with groups[i].
Your task is to select the longest alternating subsequence 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.
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.
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"]
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.
Example 2:
Input: words = ["a","b","c","d"], groups = [1,0,1,1]
Output: ["a","b","c"]
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.
Constraints:
1 <= n == words.length == groups.length <= 1001 <= words[i].length <= 10groups[i] is either 0 or 1.words consists of distinct strings.words[i] consists of lowercase English letters.The greedy approach involves traversing the groups array and selecting elements that form an alternating pattern. We can iterate through the array, keeping track of the last selected group's value and select the current word if its corresponding group value is different from the last selected value. This approach ensures that we build a valid alternating subsequence as we proceed through each element.
This C code iterates through the array of words and selects words whose corresponding group values alternate. It uses the variable lastGroup to track the group value of the last selected word. Whenever a new word is added to the result, it updates lastGroup with the current group's value. This way, we form a subsequence where consecutive word indexes have differing group values.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the length of the array. We make a single pass through the array.
Space Complexity: O(n) for storing the result.
This approach involves using Dynamic Programming to construct the longest alternating subsequence. By maintaining a DP table, we can determine the longest subsequence ending at each index, using previously calculated results. This approach efficiently decides when to extend the subsequence ending at a given position.
The above C code uses nested loops to populate a `dp` array, where each element at index i indicates the maximum length of an alternating subsequence that ends at that position. The solution reconstructs this subsequence from the `dp` states, finding the longest sequence possible.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) due to the nested iteration.
Space Complexity: O(n), required for the `dp` array.
| Approach | Complexity |
|---|---|
| Greedy Approach | Time Complexity: O(n), where n is the length of the array. We make a single pass through the array. |
| Dynamic Programming Approach | Time Complexity: O(n^2) due to the nested iteration. |
I solved too many Leetcode problems • NeetCodeIO • 101,495 views views
Watch 9 more video solutions →Practice Longest Unequal Adjacent Groups Subsequence I with our built-in code editor and test cases.
Practice on FleetCode