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 [i0, i1, ..., ik-1] having length k, the following holds:
For adjacent indices in the subsequence, their corresponding groups are unequal, i.e., groups[ij] != groups[ij+1], for each j where 0 < j + 1 < k.
words[ij] and words[ij+1] 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.
Input: words =["bab","dab","cab"], groups =[1,2,2]Output:["bab","cab"]Explanation: A subsequence that can be selected is`[0,2]`.*`groups[0] != groups[2]`*`words[0].length == words[2].length`, and the hamming distance between them is1.So, a valid answer is`[words[0],words[2]] = ["bab","cab"]`.Another subsequence that can be selected is`[0,1]`.*`groups[0] != groups[1]`*`words[0].length == words[1].length`, and the hamming distance between them is`1`.So, another valid answer is`[words[0],words[1]] = ["bab","dab"]`.It can be shown that the length of the longest subsequence of indices that
satisfies the conditions is`2`.
Example 2:
1
2
3
4
5
6
7
8
9
10
Input: words =["a","b","c","d"], groups =[1,2,3,4]Output:["a","b","c","d"]Explanation: We can select the subsequence `[0,1,2,3]`.It satisfies both conditions.Hence, the answer is`[words[0],words[1],words[2],words[3]] =
["a","b","c","d"]`.It has the longest length among all subsequences of indices that satisfy the
conditions.Hence, it is the only answer.
classSolution {
public List<String>getWordsInLongestSubsequence(
String[] words, int[] groups
) {
int n = words.length;
int[] dp =newint[n]; // Tracks length of the longest subsequenceint[] prev =newint[n]; // Backtracking from longest subsequence Arrays.fill(dp, 1); // Base case: every word is a subsequence of length 1 Arrays.fill(prev, -1);
int maxIndex = 0; // Track the index of the longest subsequence// Build up the DP array by iterating through all pairs of indicesfor (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (isValid(words[i], words[j], groups[i], groups[j]) && dp[j]+ 1 > dp[i]) {
dp[i]= dp[j]+ 1;
prev[i]= j; // Connect sequences for backtracking }
}
// Update the index with the maximum subsequence lengthif (dp[i]> dp[maxIndex]) {
maxIndex = i;
}
}
// Backtrack from the index with the longest subsequence List<String> subsequence =new ArrayList<>();
for (int i = maxIndex; i >= 0; i = prev[i]) {
subsequence.add(words[i]);
}
Collections.reverse(subsequence); // Reverse to maintain orderreturn subsequence;
}
// Helper function: checks if two words can be part of the subsequenceprivatebooleanisValid(String word1, String word2, int group1, int group2) {
if (word1.length() != word2.length() || group1 == group2) returnfalse;
int diff = 0;
for (int i = 0; i < word1.length(); i++) {
if (word1.charAt(i) != word2.charAt(i)) diff++;
if (diff > 1) returnfalse; // Ensure only one character differs }
return diff == 1; // Return true if precisely one difference exists }
}
classSolution:
defgetWordsInLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
n = len(words)
dp = [1] * n # Tracks the length of the longest subsequence prev = [-1] * n # Tracks previous index in the subsequence max_index =0# Index of the longest subsequence# Helper function: Check if two words satisfy conditionsdefis_valid(word1: str, word2: str, group1: int, group2: int) -> bool:
if len(word1) != len(word2) or group1 == group2:
returnFalse diff = sum(c1 != c2 for c1, c2 in zip(word1, word2)) # Count character differencesreturn diff ==1# Build DP array and track previous indicesfor i in range(1, n):
for j in range(i):
if is_valid(words[i], words[j], groups[i], groups[j]) and dp[j] +1> dp[i]:
dp[i] = dp[j] +1 prev[i] = j # Update backtracking informationif dp[i] > dp[max_index]:
max_index = i # Update maximum subsequence index# Backtrack to construct the subsequence subsequence = []
while max_index !=-1:
subsequence.append(words[max_index])
max_index = prev[max_index]
return subsequence[::-1] # Reverse to maintain order