You are given two strings wordi and word2. A string x is called almost equal to y if you…
- User
You are given two strings wordi and word2. A string x is called almost equal to y if you can change at most one character in x to make it identical to y. A sequence of indices seq is called valid if: • The indices are sorted in ascending order. • Concatenating the characters at these indices in word in the same order results in a string that is almost equal to word2. Return an array of size word2. length representing the lexicographically smallest valid sequence of indices. If no such sequence of indices exists, return an empty array. Note that the answer must represent the lexicographically smallest array, not the corresponding string formed by those indices. Example 1: Input: word1 = "vbcca", word2 = "abc" Output: [0,1,2] Explanation: The lexicographically smallest valid sequence of indices is [0, 1, 2]: • Change word1 [0] to ('a'. • word1 [1] is already ('b'. • word1[2] is already 'c'. Example 2: Input: word1 = "bacdc", word2 = "abc" Output: [1,2,4] Explanation: The lexicographically smallest valid sequence of indices is [1, 2, 4]: • word1[1] is already 'a'. • Change word1[2] to 'b'. • word1 [4] is already ('c') Example 3: Input: word1 = "aaaaaa", word2 = "aaabc" Output: [] Explanation: There is no valid sequence of indices. Example 4: Input: word1 = "abc", word2 = "ab" Output: [0,1] Constraints: • 1 <= word2. length < word1. length <= 3 * 10^5 • word1 and word2 consist only of lowercase English letters.vector validSequence(string word1, string word2) { int n = word1.size(); int m = word2.size();
// Edge case: if word2 is longer than word1, impossible to form if(m > n) return {}; // Precompute character indices for each character in word1 vector<vector<int>> char_indices(26, vector<int>()); for(int i = 0; i < n; ++i){ char_indices[word1[i] - 'a'].push_back(i); } // Precompute next_not_c[c][x]: for each character c, the next index >=x where word1[x] != c vector<vector<int>> next_not_c(26, vector<int>(n+1, n)); for(int c = 0; c < 26; ++c){ next_not_c[c][n] = n; // Beyond the last index for(int x = n-1; x >= 0; --x){ if(word1[x] != ('a' + c)){ next_not_c[c][x] = x; } else{ next_not_c[c][x] = next_not_c[c][x+1]; } } } // Initialize DP arrays const int INF = n + 1; vector<int> dp0(m+1, INF); // dp0[j]: smallest index in word1 to match first j chars with 0 mismatches vector<int> dp1(m+1, INF); // dp1[j]: smallest index in word1 to match first j chars with 1 mismatch dp0[0] = -1; // Before starting, previous index is -1 // To reconstruct the path, maintain parent pointers struct Parent { int prev_j; // previous position in word2 int index; // index in word1 int k; // 0 from dp0, 1 from dp1 }; vector<Parent> parent0(m+1, { -1, -1, -1 }); vector<Parent> parent1(m+1, { -1, -1, -1 }); for(int j = 0; j < m; ++j){ char c = word2[j]; int ci = c - 'a'; // ----- Update dp0[j+1] ----- if(dp0[j] != INF){ // Find the first index in word1 > dp0[j] where word1[x] == word2[j] auto &list_c = char_indices[ci]; auto it = upper_bound(list_c.begin(), list_c.end(), dp0[j]); if(it != list_c.end()){ int x = *it; if(x < dp0[j+1]){ dp0[j+1] = x; parent0[j+1] = { j, x, 0 }; } } // ----- Update dp1[j+1] via mismatch ----- // Find the first index in word1 > dp0[j] where word1[x] != word2[j] int x_a = next_not_c[ci][dp0[j] + 1]; if(x_a < dp1[j+1] && x_a < n){ if(x_a < dp1[j+1]){ dp1[j+1] = x_a; parent1[j+1] = { j, x_a, 0 }; // Came from dp0[j] } } } // ----- Update dp1[j+1] via match from dp1[j] ----- if(dp1[j] != INF){ // Find the first index in word1 > dp1[j] where word1[x] == word2[j] auto &list_c = char_indices[ci]; auto it = upper_bound(list_c.begin(), list_c.end(), dp1[j]); if(it != list_c.end()){ int x_b = *it; if(x_b < dp1[j+1]){ dp1[j+1] = x_b; parent1[j+1] = { j, x_b, 1 }; // Came from dp1[j] } } } } // Function to reconstruct the sequence auto reconstruct = [&](int j, int k) -> vector<int> { vector<int> sequence(j); while(j > 0){ if(k == 0){ Parent p = parent0[j]; sequence[j-1] = p.index; j = p.prev_j; k = p.k; // 0: from dp0, 1: from dp1 } else{ Parent p = parent1[j]; sequence[j-1] = p.index; j = p.prev_j; k = p.k; } } return sequence; }; // Reconstruct both sequences if possible vector<int> seq0, seq1; bool has0 = dp0[m] != INF; bool has1 = dp1[m] != INF; if(has0 && has1){ seq0 = reconstruct(m, 0); seq1 = reconstruct(m, 1); // Compare lex order if(seq0 < seq1){ return seq0; } else{ return seq1; } } else if(has0){ return reconstruct(m, 0); } else if(has1){ return reconstruct(m, 1); } else{ return {}; } solve this error in code Wrong Answer | 548 / 902 testcases passed - Assistant