Skip to content

2901. Longest Unequal Adjacent Groups Subsequence II #1691

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the longest subsequence of indices from given arrays words and groups such that adjacent elements in the subsequence have different groups and their corresponding words have a Hamming distance of 1. The solution involves dynamic programming to track the longest valid subsequence ending at each index and backtracking to reconstruct the subsequence.

Approach

  1. Dynamic Programming (DP) Setup: Use a DP array where dp[i] represents the length of the longest valid subsequence ending at index i. Initialize each element of dp to 1 since each element by itself is a valid subsequence of length 1.
  2. Track Previous Indices: Maintain a prev array to track the previous index in the subsequ…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim May 16, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants