LeetCode Problem Workspace

Find the Lexicographically Smallest Valid Sequence

Determine the lexicographically smallest valid index sequence by using state transition dynamic programming over word1 and word2.

category

4

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Medium · State transition dynamic programming

bolt

Answer-first summary

Determine the lexicographically smallest valid index sequence by using state transition dynamic programming over word1 and word2.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for State transition dynamic programming

Try AiBox Copilotarrow_forward

This problem requires constructing the lexicographically smallest sequence of indices from word1 matching word2 under almost-equal rules. Use state transition dynamic programming to track suffix matches and efficiently decide the minimal sequence. Combining Two Pointers with DP ensures each choice respects both order and lexicographic constraints.

Problem Statement

You are given two strings, word1 and word2, containing only lowercase English letters. A string x is considered almost equal to y if changing at most one character in x makes it identical to y. Your task is to identify a sequence of indices in word1 that forms word2 under these almost-equal conditions.

A sequence of indices seq is valid if for each character in word2 there exists a corresponding character in word1 at the given index or one change away, maintaining the original order. Return the lexicographically smallest sequence of indices, or an empty array if no valid sequence exists. Constraints: 1 <= word2.length < word1.length <= 3 * 10^5.

Examples

Example 1

Input: word1 = "vbcca", word2 = "abc"

Output: [0,1,2]

The lexicographically smallest valid sequence of indices is [0, 1, 2] :

Example 2

Input: word1 = "bacdc", word2 = "abc"

Output: [1,2,4]

The lexicographically smallest valid sequence of indices is [1, 2, 4] :

Example 3

Input: word1 = "aaaaaa", word2 = "aaabc"

Output: []

There is no valid sequence of indices.

Constraints

  • 1 <= word2.length < word1.length <= 3 * 105
  • word1 and word2 consist only of lowercase English letters.

Solution Approach

Precompute suffix matches with DP

Define dp[i] as the longest suffix of word2 that exists as a subsequence in the suffix of word1 starting at index i. This state transition allows checking for almost-equal matches efficiently and reduces redundant comparisons.

Iterate with greedy choice using Two Pointers

Move through word1 with two pointers, selecting the earliest index that extends the valid subsequence of word2. At each step, prefer the smallest character index that maintains the DP state to guarantee lexicographic minimality.

Backtrack or construct the result

After building DP and pointer decisions, construct the final sequence by following chosen indices. Skip indices that cannot extend word2's subsequence or violate the almost-equal constraint to ensure the final sequence is valid and minimal.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity depends on building dp[i] for each suffix and iterating with two pointers, potentially O(word1.length * word2.length). Space complexity is dominated by dp array and auxiliary arrays for character positions.

What Interviewers Usually Probe

  • Check if the candidate correctly defines the DP state for suffix matching.
  • Listen for a greedy step combined with DP to ensure lexicographic minimality.
  • Verify handling of almost-equal condition and empty sequence returns.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring the almost-equal rule when choosing characters from word1.
  • Not maintaining lexicographic order while constructing the result.
  • Inefficient DP that recomputes suffix matches repeatedly.

Follow-up variants

  • Compute the lexicographically largest valid sequence instead of smallest.
  • Allow up to k character changes in the almost-equal definition.
  • Return all valid sequences rather than only the minimal one.

FAQ

What pattern does 'Find the Lexicographically Smallest Valid Sequence' follow?

It follows a state transition dynamic programming pattern combined with greedy selection to maintain lexicographic order.

How do I handle almost-equal character rules in this problem?

Ensure that for each character in word2, you can change at most one character in word1's selected index to match it, using DP to track possibilities.

Can this approach handle large strings efficiently?

Yes, by precomputing suffix matches with DP and using Two Pointers for greedy selection, the solution scales to word1 length up to 3 * 10^5.

What should I do if no valid sequence exists?

Return an empty array to indicate no valid indices meet the almost-equal and order requirements.

Why is lexicographic minimality important here?

Choosing the smallest possible indices ensures the sequence is minimal in dictionary order, which is the core requirement of the problem.

terminal

Solution

Solution 1

#### Python3

1
Find the Lexicographically Smallest Valid Sequence Solution: State transition dynamic programming | LeetCode #3302 Medium