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 of word with size m starting at index i is equal to str2, i.e., word[i..(i + m - 1)] == str2.
  • If str1[i] == 'F', the substring of word with size m starting at index i is not equal to str2, 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
Lexicographically Smallest Generated String INPUT str1 = "TF" T F i=0 i=1 str2 = "ab" a b n = 2, m = 2 word length = n + m - 1 = 3 Conditions: T at i=0: word[0:2] must = "ab" F at i=1: word[1:3] must != "ab" word = [ _ ][ _ ][ _ ] ALGORITHM STEPS 1 Process T at i=0 Place str2 at pos 0-1 a b ? 2 Process F at i=1 word[1:3] must != "ab" 3 Greedy: Try smallest Try 'a' for pos 2 word[1:3] = "ba" != "ab" OK 4 Validate result All conditions satisfied Result Building: a b a "aba" is smallest valid FINAL RESULT Generated Word: a b a 0 1 2 Output: "aba" Verification: T at i=0: word[0:2] = "ab" = str2 OK F at i=1: word[1:3] = "ba" != str2 OK All conditions satisfied! Key Insight: The greedy approach tries the lexicographically smallest character ('a') for each unfilled position. For T conditions, we must place str2 exactly. For F conditions, we ensure the substring differs. By greedily choosing 'a' at position 2, we get "ba" at [1:3] which differs from "ab", satisfying F. TutorialsPoint - Lexicographically Smallest Generated String | Greedy Character Placement
Asked in
Google 15 Meta 12 Microsoft 8
8.9K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen