3302. Find the Lexicographically Smallest Valid Sequence
Difficulty: Medium
Topics: Staff, Two Pointers, String, Dynamic Programming, Greedy, Biweekly Contest 140
You are given two strings word1 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
word1in the same order results in a string that is almost equal toword2.
Return an array of size word2.length representing the lexicographically smallest1 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'.
- The lexicographically smallest valid sequence of indices is
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'.
- The lexicographically smallest valid sequence of indices is
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]
Example 5:
- Input: word1 = "ab", word2 = "a"
- Output: [0]
Example 6:
- Input: word1 = "ab", word2 = "c"
- Output: [0]
Example 7:
- Input: word1 = "ab", word2 = "d"
- Output: [0]
Example 8:
- Input: word1 = "abc", word2 = "ac"
- Output: [0,2]
Example 9:
- Input: word1 = "abc", word2 = "bd"
- Output: [1,2]
Example 10:
- Input: word1 = "xyz", word2 = "abc"
- Output: []
Example 11:
- Input: word1 = "aaaa", word2 = "aa"
- Output: [0,1]
Example 12:
- Input: word1 = "bbbb", word2 = "bb"
- Output: [0,1]
Constraints:
1 <= word2.length < word1.length <= 3 * 10⁵-
word1andword2consist only of lowercase English letters.
Hint:
- Let
dp[i]be the longest suffix ofword2that exists as a subsequence of suffix of the substring ofword1starting at indexi. - If
dp[i + 1] < mandword1[i] == word2[m - dp[i + 1] - 1],dp[i] = dp[i + 1] + 1. Otherwise,dp[i] = dp[i + 1]. - For each index
i, greedily select characters using thedparray to know whether a solution exists.
Solution:
We present a greedy two‑phase algorithm that constructs the lexicographically smallest valid index sequence for matching word2 as a subsequence of word1 with at most one character change. The algorithm first precomputes the latest possible match positions for each character of word2 from the right (last array), then scans word1 left‑to‑right, greedily taking matches whenever possible, and allowing exactly one substitution only when it can still lead to a complete solution.
Approach
-
Precompute suffix last‑occurrence (
last)- Traverse
word1from right to left andword2from right to left. - For each character of
word2(starting from the last), record the rightmost index inword1where it can be matched. - This array tells us for each position
jinword2, the latest index inword1whereword2[j]can appear as a subsequence suffix.
- Traverse
-
Greedy left‑to‑right selection
- Maintain a pointer
jfor the current position inword2. - Maintain a flag
canSkip(initializedtrue) indicating we still have the one allowed change. - Iterate
ifrom 0 ton-1:- If
j == m, we have matched all characters, break. - If
word1[i] == word2[j], take this index (exact match), advancej. - Else if we have not used the change yet (
canSkip) and it is safe to change (eitherjis the last character ofword2, oriis before the latest possible match for the rest ofword2starting atj+1), then we use the change: setans[j] = i, markcanSkip = false, advancej.
- If
- After the loop, if
j == m, return the answer; otherwise return empty array.
- Maintain a pointer
-
Why it yields the lexicographically smallest sequence
- By scanning from left to right and taking the earliest possible index for each position (exact match or the allowed change), we minimise each element of the resulting array in turn.
- The safety check using
lastensures that using a change at the current position does not prevent a complete match for the remaining suffix.
Let's implement this solution in PHP: 3302. Find the Lexicographically Smallest Valid Sequence
<?php
/**
* @param String $word1
* @param String $word2
* @return Integer[]
*/
function validSequence(string $word1, string $word2): array
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo validSequence("vbcca", "abc") . "\n"; // Output: [0,1,2]
echo validSequence("bacdc", "abc") . "\n"; // Output: [1,2,4]
echo validSequence("aaaaaa", "aaabc") . "\n"; // Output: []
echo validSequence("abc", "ab") . "\n"; // Output: [0, 1]
echo validSequence("ab", "a") . "\n"; // Output: [0]
echo validSequence("ab", "c") . "\n"; // Output: [0]
echo validSequence("ab", "d") . "\n"; // Output: [0]
echo validSequence("abc", "ac") . "\n"; // Output: [0, 2]
echo validSequence("abc", "bd") . "\n"; // Output: [1, 2]
echo validSequence("xyz", "abc") . "\n"; // Output: []
echo validSequence("aaaa", "aa") . "\n"; // Output: [0, 1]
echo validSequence("bbbb", "bb") . "\n"; // Output: [0, 1]
?>
Explanation:
-
Step 1 – Build
lastarray:-
last[j]stores the rightmost index inword1that can serve as the start of a subsequence matchingword2[j..m-1]. - This is computed by scanning
word1from the end, matching backwards from the end ofword2.
-
-
Step 2 – Greedy matching with one change:
- For each index
iinword1, we try to match the current needed characterword2[j]. - If characters equal, we immediately take
i– this is always safe and gives the smallest possible index. - If they differ, we may use our one allowed substitution if:
- We haven't already used it (
canSkip == true). - We can still complete the rest of
word2after this change. This is guaranteed if eitherjis the last character (no more characters needed) ori < last[j+1](meaning there exists a valid subsequence for the remainder starting afteri).
- We haven't already used it (
- Using the change at the earliest possible
iminimises the current index.
- For each index
-
Step 3 – Termination check:
- If we successfully match all characters of
word2, we returnans. - Otherwise, no valid sequence exists, return
[].
- If we successfully match all characters of
Complexity Analysis
-
Time Complexity:
O(n + m)- One pass to build
last(O(n)). - One pass for greedy selection (
O(n)). - All operations are constant time per character.
- One pass to build
-
Space Complexity:
O(m)for thelastarray and the answer array.- (The answer is required output, so it counts as output space;
lastis the only auxiliary structure.)
- (The answer is required output, so it counts as output space;
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
Lexicographically Smaller: An array
ais lexicographically smaller than an arraybif in the first position whereaandbdiffer, arrayahas an element that is less than the corresponding element inb. If the firstmin(a.length, b.length)elements do not differ, then the shorter array is the lexicographically smaller one. ↩
Top comments (0)