Lexicographically Smallest Generated String - Problem

Imagine you're a puzzle master creating a word game with pattern matching rules! You have two strings: str1 (the pattern string) and str2 (the target string).

Your goal is to construct the lexicographically smallest string of length n + m - 1 that follows these rules:

  • When str1[i] == 'T': The substring starting at position i with length m must equal str2
  • When str1[i] == 'F': The substring starting at position i with length m must NOT equal str2

Think of it as placing characters one by one while respecting all the pattern constraints. If it's impossible to satisfy all conditions, return an empty string.

Example: If str1 = "TF" and str2 = "ab", you need a string of length 4 where positions 0-1 must be "ab" and positions 1-2 must NOT be "ab".

Input & Output

example_1.py โ€” Basic Pattern Matching
$ Input: str1 = "TF", str2 = "ab"
โ€บ Output: "abaa"
๐Ÿ’ก Note: We need a string of length 4. Position 0-1 must be 'ab' (T constraint), position 1-2 must NOT be 'ab' (F constraint). Starting with 'ab__', position 2 can be 'a' since 'ba' โ‰  'ab', and position 3 can be 'a' for the lexicographically smallest result.
example_2.py โ€” Conflicting Constraints
$ Input: str1 = "TT", str2 = "ab"
โ€บ Output: "aba"
๐Ÿ’ก Note: String length is 3. Both positions 0-1 and 1-2 must equal 'ab'. This forces: position 0-1 = 'ab', position 1-2 = 'ab'. The only solution is 'aba' where both constraints are satisfied.
example_3.py โ€” Impossible Case
$ Input: str1 = "TF", str2 = "aa"
โ€บ Output: ""
๐Ÿ’ก Note: We need length 4. Position 0-1 must be 'aa' (T), but position 1-2 must NOT be 'aa' (F). Starting with 'aa__', position 1-2 would be 'a?' where ? cannot be 'a' (else violates F), but positions are already fixed by T constraint, creating an impossible situation.

Visualization

Tap to expand
Pattern Constraint VisualizationInput: str1 = "TF", str2 = "ab"Need string of length 4 where pos 0-1 = "ab" and pos 1-2 โ‰  "ab"Target: Build lexicographically smallest valid stringapos 0bpos 1?pos 2?pos 3T: must be "ab"F: cannot be "ab"a"ba" โ‰  "ab" โœ“asmallestResult: "abaa" - Lexicographically smallest valid string
Understanding the Visualization
1
Set Required Pieces
Place all mandatory patterns first (T constraints)
2
Check Conflicts
Ensure forbidden patterns don't accidentally appear (F constraints)
3
Fill Gaps Optimally
Complete remaining positions with smallest valid letters
4
Validate Solution
Verify final string satisfies all original constraints
Key Takeaway
๐ŸŽฏ Key Insight: Use constraint propagation by handling mandatory placements first, then ensure conflicts are resolved, and finally fill gaps greedily for lexicographically smallest result.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(26^(n+m-1) * n * m)

Generate 26^(n+m-1) strings, each taking O(n*m) time to validate all constraints

n
2n
โœ“ Linear Growth
Space Complexity
O(n + m)

Space for storing the current string and str2 for comparisons

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค str1.length โ‰ค 103
  • 1 โ‰ค str2.length โ‰ค 103
  • str1[i] is either 'T' or 'F'
  • str2 consists only of lowercase English letters
  • str1.length + str2.length - 1 โ‰ค 103
Asked in
Google 24 Meta 18 Amazon 15 Microsoft 12
26.2K Views
Medium Frequency
~35 min Avg. Time
847 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