Longest Unequal Adjacent Groups Subsequence II - Problem

You are given a string array words and an array groups, both arrays having length n.

The hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different.

You need to select the longest subsequence from an array of indices [0, 1, ..., n - 1], such that for the subsequence denoted as [i₀, i₁, ..., iₖ₋₁] having length k, the following holds:

  • For adjacent indices in the subsequence, their corresponding groups are unequal, i.e., groups[iⱼ] != groups[iⱼ₊₁], for each j where 0 ≤ j + 1 < k.
  • words[iⱼ] and words[iⱼ₊₁] are equal in length, and the hamming distance between them is 1, where 0 ≤ j + 1 < k, for all indices in the subsequence.

Return a string array containing the words corresponding to the indices (in order) in the selected subsequence. If there are multiple answers, return any of them.

Note: strings in words may be unequal in length.

Input & Output

Example 1 — Basic Valid Chain
$ Input: words = ["a","b","c","d"], groups = [1,0,1,1]
Output: ["a","b","c"]
💡 Note: Chain a→b→c: groups [1,0,1] are alternating, hamming distances are 1 (a≠b at 1 position, b≠c at 1 position). Length = 3.
Example 2 — Different Length Words
$ Input: words = ["a","ab","abc"], groups = [0,0,1]
Output: ["ab"]
💡 Note: Words have different lengths so cannot form valid chain with hamming distance constraint. Best subsequence has length 1.
Example 3 — Same Groups
$ Input: words = ["abc","def"], groups = [0,0]
Output: ["abc"]
💡 Note: Both words have same group (0), so cannot form chain. Maximum subsequence length is 1.

Constraints

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 10
  • groups.length == words.length
  • 0 ≤ groups[i] ≤ 1

Visualization

Tap to expand
Longest Unequal Adjacent Groups Subsequence II INPUT words[] array: "a" i=0 "b" i=1 "c" i=2 "d" i=3 groups[] array: 1 0 1 1 Constraints: 1. Adjacent groups must differ 2. Words must have equal length 3. Hamming distance = 1 Hamming("a","b") = 1 (1 char differs at same position) Hamming("b","c") = 1 ALGORITHM (DP) 1 Initialize DP dp[i] = longest ending at i prev[i] = track path 2 For each pair (i,j) Check: groups[i] != groups[j] Check: len(words[i])==len(words[j]) 3 Check Hamming If hamming(i,j) == 1: Update dp[j] = dp[i] + 1 4 Backtrack Find max dp[i], trace prev[] DP Table: i=0 i=1 i=2 i=3 1 2 3 1 Max dp = 3 at index 2 FINAL RESULT Selected Subsequence: "a" g=1 "b" g=0 "c" g=1 Validation: "a" --> "b": groups 1!=0 [OK] hamming=1 [OK] "b" --> "c": groups 0!=1 [OK] Why not "d"? groups[2]=1 == groups[3]=1 Adjacent groups must differ! Output: ["a","b","c"] Key Insight: Use Dynamic Programming to track the longest valid subsequence ending at each index. For each pair of indices, check three conditions: different groups, equal word lengths, and Hamming distance of exactly 1. Backtrack from the maximum dp value to reconstruct the answer. Time complexity: O(n^2 * L) where L = max word length. TutorialsPoint - Longest Unequal Adjacent Groups Subsequence II | DP Approach
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen