Watch 10 video solutions for Lexicographically Smallest Generated String, a hard level problem involving String, Greedy, String Matching. This walkthrough by codestorywithMIK has 9,453 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
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.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 "".
Example 1:
Input: str1 = "TFTF", str2 = "ab"
Output: "ababa"
Explanation:
"ababa"| Index | T/F | Substring of length m |
|---|---|---|
| 0 | 'T' |
"ab" |
| 1 | 'F' |
"ba" |
| 2 | 'T' |
"ab" |
| 3 | 'F' |
"ba" |
The strings "ababa" and "ababb" can be generated by str1 and str2.
Return "ababa" since it is the lexicographically smaller string.
Example 2:
Input: str1 = "TFTF", str2 = "abc"
Output: ""
Explanation:
No string that satisfies the conditions can be generated.
Example 3:
Input: str1 = "F", str2 = "d"
Output: "a"
Constraints:
1 <= n == str1.length <= 1041 <= m == str2.length <= 500str1 consists only of 'T' or 'F'.str2 consists only of lowercase English characters.Problem Overview: You need to generate the lexicographically smallest string that satisfies a set of constraints describing which substrings must match or must not match a given pattern. The challenge is balancing correctness with lexicographic minimality while enforcing substring conditions efficiently.
Approach 1: Brute Force Generation with Validation (Exponential time, O(k^n) time, O(n) space)
Start by generating candidate strings character by character from the alphabet (typically 'a' to 'z'). For each fully built string, check whether all substring constraints are satisfied. Validation usually involves comparing substrings against the pattern directly. While this approach guarantees correctness, the search space grows exponentially and quickly becomes infeasible for realistic input sizes.
This brute force method helps clarify the problem: you must satisfy matching rules while minimizing lexicographic order. However, repeatedly rebuilding and checking substrings results in excessive work, making it unsuitable beyond very small inputs.
Approach 2: Greedy Construction with String Matching (O(n + m) time, O(n) space)
The optimal strategy builds the answer from left to right using a greedy rule: always place the smallest possible character that keeps the constraints satisfiable. Instead of repeatedly comparing substrings, use efficient string matching techniques such as the KMP prefix function to track how much of the pattern currently matches.
When a constraint requires a specific substring match, the algorithm forces those characters into the result. For positions without strict requirements, try characters from 'a' upward and verify that placing them does not create a forbidden pattern match. The prefix-function state lets you update matches in constant time per character.
This combination of greedy selection and incremental string matching ensures that each position is processed once while maintaining correctness. The greedy rule guarantees lexicographic minimality because you only choose larger characters when smaller ones would violate constraints.
Recommended for interviews: Interviewers expect the greedy + string matching approach. Starting with brute force shows you understand the constraints, but the optimized solution demonstrates knowledge of prefix functions and efficient substring checking—key skills for advanced string problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Generation | O(k^n) | O(n) | Useful only for understanding constraints or very small inputs |
| Greedy + String Matching (KMP) | O(n + m) | O(n) | General optimal solution; efficient substring validation during construction |