#2900

Longest Unequal Adjacent Groups Subsequence I

easy · 67% accepted · 483 likes · top 73%

array · string · dynamic programming · greedy

Description

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 (that is, there cannot be consecutive 0 or 1).

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.

Solution