Skip to content

Practice Lexicographically Smallest Generated String

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

Hard#3474
LeetCode Problem

Lexicographically Smallest Generated String

Problem Statement

Given str1 of length n and str2 of length m, construct the lexicographically smallest lowercase string word of length n + m - 1. For each index i, the length-m substring starting at i must equal str2 when str1[i] is T and must differ from str2 when str1[i] is F. Return word, or the empty string if no valid construction exists.

Source: Lexicographically Smallest Generated String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
str1 = "TFTF", str2 = "ab"
Output
"ababa"

# Example 2

Input
str1 = "TFTF", str2 = "abc"
Output
""

# Example 3

Input
str1 = "F", str2 = "d"
Output
"a"

Constraints

1 <= n == str1.length <= 10^4
1 <= m == str2.length <= 500
str1 consists only of 'T' or 'F'.
str2 consists only of lowercase English characters.

Related LeetCode Problems

Practice more problems similar to Lexicographically Smallest Generated String.

Review your practice with PhantomCodeAI

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