Lexicographically Smallest Generated String - Problem
You are given two strings str1 and str2 of lengths n and m respectively.
A string word of length n + m - 1 is defined to be generated by str1 and str2 if it satisfies the following conditions for each index 0 <= i <= n - 1:
- If
str1[i] == 'T', the substring ofwordwith sizemstarting at indexiis equal tostr2, i.e.,word[i..(i + m - 1)] == str2. - If
str1[i] == 'F', the substring ofwordwith sizemstarting at indexiis not equal tostr2, i.e.,word[i..(i + m - 1)] != str2.
Return the lexicographically smallest possible string that can be generated by str1 and str2. If no string can be generated, return an empty string "".
Input & Output
Example 1 — Basic T and F constraints
$
Input:
str1 = "TF", str2 = "ab"
›
Output:
"aba"
💡 Note:
Need string of length 3. At position 0, must have "ab" (T constraint). At position 1, must NOT have "ab" (F constraint). String "aba" satisfies both: substring [0,1] = "ab" ✓, substring [1,2] = "ba" ≠ "ab" ✓
Example 2 — Multiple T constraints
$
Input:
str1 = "TT", str2 = "aa"
›
Output:
"aaa"
💡 Note:
Need string of length 3. Both positions 0 and 1 require "aa". Only "aaa" works: substring [0,1] = "aa" ✓, substring [1,2] = "aa" ✓
Example 3 — Impossible constraints
$
Input:
str1 = "TF", str2 = "aa"
›
Output:
""
💡 Note:
Need string of length 3. Position 0 requires "aa" and position 1 forbids "aa". But if characters [0,1] = "aa", then characters [1,2] must also be "aa" (overlapping). This creates contradiction, so no valid string exists.
Constraints
- 1 ≤ str1.length ≤ 1000
- 1 ≤ str2.length ≤ 26
- str1 consists only of 'T' and 'F'
- str2 consists only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
str1 defines T/F constraints, str2 is pattern to match/avoid
2
Process
Check each position against constraints to build valid string
3
Output
Return lexicographically smallest string satisfying all constraints
Key Takeaway
🎯 Key Insight: Use greedy character placement while respecting T/F pattern constraints at each position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code