Skip to content

Practice Find the Lexicographically Smallest Valid Sequence

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3302
LeetCode Problem

Find the Lexicographically Smallest Valid Sequence

Problem Statement

Given word1 and word2, choose strictly increasing indices in word1 whose characters form a string differing from word2 in at most one position. Return the lexicographically smallest valid array of indices, with length word2.length, or an empty array if none exists. Minimize the index array itself, not the selected character string.

Source: Find the Lexicographically Smallest Valid Sequence on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
word1 = "vbcca", word2 = "abc"
Output
[0,1,2]

# Example 2

Input
word1 = "bacdc", word2 = "abc"
Output
[1,2,4]

# Example 3

Input
word1 = "aaaaaa", word2 = "aaabc"
Output
[]

# 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.

Related LeetCode Problems

Practice more problems similar to Find the Lexicographically Smallest Valid Sequence.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.