Longest Unequal Adjacent Groups Subsequence II - Problem

Imagine you're a linguist studying word evolution patterns! You have an array of words and a corresponding groups array that categorizes each word.

Your mission is to find the longest subsequence where:

  • ๐Ÿ”„ Adjacent groups alternate: groups[i] != groups[j] for consecutive elements
  • ๐Ÿ“ Words have equal length: Adjacent words in your subsequence must be the same length
  • ๐ŸŽฏ Hamming distance is exactly 1: Adjacent words differ by exactly one character

The hamming distance between two equal-length strings is the number of positions where characters differ. For example, "abc" and "aec" have hamming distance 1.

Return the words corresponding to the longest valid subsequence. If multiple answers exist, return any of them.

Input & Output

example_1.py โ€” Basic alternating groups
$ Input: words = ["bab", "dab", "cab"], groups = [1, 2, 2]
โ€บ Output: ["bab", "dab"]
๐Ÿ’ก Note: "bab" (group 1) and "dab" (group 2) have different groups, same length (3), and hamming distance 1 (only 'b' vs 'd' differs). "cab" cannot be added because it has the same group as "dab".
example_2.py โ€” Single character differences
$ Input: words = ["a", "b", "c", "d"], groups = [1, 2, 1, 2]
โ€บ Output: ["a", "b", "c", "d"]
๐Ÿ’ก Note: All adjacent pairs satisfy the conditions: different groups, same length (1), and hamming distance 1. This forms the longest possible alternating sequence.
example_3.py โ€” No valid extensions
$ Input: words = ["abc", "def"], groups = [1, 1]
โ€บ Output: ["abc"]
๐Ÿ’ก Note: Both words belong to the same group, so no valid alternating subsequence of length > 1 exists. Return any single word.

Constraints

  • 1 โ‰ค words.length โ‰ค 1000
  • 1 โ‰ค words[i].length โ‰ค 10
  • words[i] consists of lowercase English letters only
  • 1 โ‰ค groups[i] โ‰ค 105
  • Hamming distance is only defined for equal-length strings

Visualization

Tap to expand
Word Evolution ChainbabGroup 1dabGroup 2cabGroup 2โœ“ ValidDiff groups, hamming=1โœ— InvalidSame groupsDynamic Programming ProcessStep 1: Initializedp[0]=1, dp[1]=1, dp[2]=1Step 2: Extenddp[1] = dp[0] + 1 = 2Step 3: ResultBest: ["bab", "dab"]Hamming Distance Check"bab" vs "dab": positions differ at index 0 onlyb โ‰  d, a = a, b = b โ†’ distance = 1 โœ“
Understanding the Visualization
1
Identify Candidates
Find all words that can potentially connect (different groups, same length)
2
Check Mutations
Verify hamming distance is exactly 1 (single character change)
3
Build Chains
Use DP to find longest valid evolutionary chain
4
Reconstruct Path
Follow parent pointers to get the actual word sequence
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to build optimal subsequences by extending previous valid chains, checking three conditions: different groups, equal length, and hamming distance of exactly 1.
Asked in
Google 42 Meta 38 Amazon 25 Microsoft 18
52.1K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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