Magicsheet logo

Longest Unequal Adjacent Groups Subsequence II

Medium
100%
Updated 6/1/2025

Longest Unequal Adjacent Groups Subsequence II

What is this problem about?

The Longest Unequal Adjacent Groups Subsequence II is a significantly harder variation of Part I. You are again given an array of words and an array of groups. You must form the longest subsequence where adjacent chosen words have different groups. However, a new strict rule is added: adjacent chosen words must also have the exact same length, and their characters can differ by at most one position (Hamming distance of 1).

Why is this asked in interviews?

This problem is a robust test of a candidate's ability to model complex states using Dynamic Programming (DP) or Graph algorithms. It forces candidates to synthesize multiple constraints (alternating groups, identical lengths, and exactly one character difference). Interviewers use it to gauge whether a senior candidate can handle multi-layered string comparison logic inside an O(N2)O(N^2) DP loop.

Algorithmic pattern used

This problem relies heavily on Dynamic Programming. It is fundamentally similar to the Longest Increasing Subsequence (LIS) pattern. We create a dp array where dp[i] represents the length of the longest valid subsequence ending at index i. For each word at i, we check all previous words at index j. If they meet all three conditions (different group, same length, Hamming distance of 1), we update dp[i] = max(dp[i], dp[j] + 1). We also maintain a parent array to reconstruct the actual words at the end.

Example explanation

Words: ["cat", "bat", "rat", "dog"] Groups: [0, 1, 0, 1]

  • "cat" (Grp 0). dp[0] = 1.
  • "bat" (Grp 1). Lengths match (3). Grps differ (0 vs 1). Hamming dist is 1 ('c' vs 'b'). Valid! dp[1] = dp[0] + 1 = 2. Parent of 1 is 0.
  • "rat" (Grp 0).
    • Compare with "cat" (Grp 0): Grps are the same. Invalid.
    • Compare with "bat" (Grp 1): Lengths match (3). Grps differ (1 vs 0). Hamming dist is 1 ('b' vs 'r'). Valid! dp[2] = dp[1] + 1 = 3. Parent of 2 is 1.
  • "dog" (Grp 1). Lengths match, but Hamming distance to any previous word is >1>1. dp[3] = 1. Longest sequence ends at "rat" (length 3). Following parents back: "rat" -> "bat" -> "cat".

Common mistakes candidates make

A frequent mistake is calculating the Hamming distance inefficiently. If you don't check the lengths first before comparing strings character by character, you will throw IndexOutOfBounds exceptions. Another common error is returning just the length of the sequence instead of reconstructing the actual array of strings using a parent/backtracking array, which is explicitly required by the prompt.

Interview preparation tip

To master the Longest Unequal Adjacent Groups Subsequence II coding problem, make sure you are extremely comfortable reconstructing paths from a DP array. The parent[i] = j array technique is essential for any DP problem that asks you to return the final list of elements rather than just an integer length.

Similar Questions