3474. Lexicographically Smallest Generated String
Description
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 "".
Β
Example 1:
Input: str1 = "TFTF", str2 = "ab"
Output: "ababa"
Explanation:
The table below represents the string "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 <= 500str1consists only of'T'or'F'.str2consists only of lowercase English characters.
Solutions
Solution 1: Greedy
Let \(str1\) be \(s\) and \(str2\) be \(t\).
We can use a string \(ans\) of length \(n + m - 1\) to store the generated string, where each character of \(ans\) is initially set to \('a'\). We also need a boolean array \(fixed\) of length \(n + m - 1\) to record which positions in \(ans\) have already been fixed.
First, we iterate through the string \(s\). For each index \(i\), if \(s[i]\) is 'T', we need to set the substring of \(ans\) starting at index \(i\) with length \(m\) to \(t\). During this process, if we find that a position has already been fixed and the character does not match the corresponding character in \(t\), it means it is impossible to generate a valid string, so we return an empty string immediately.
Next, we iterate through \(s\) again. For each index \(i\), if \(s[i]\) is 'F', we need to check whether the substring of \(ans\) starting at index \(i\) with length \(m\) is equal to \(t\). If it is, we need to find a position in this substring and change its character to 'b' (since 'b' is lexicographically greater than 'a'), to ensure this substring is not equal to \(t\). If we cannot find such a position, it means it is impossible to generate a valid string, so we return an empty string immediately.
Finally, we concatenate the characters in \(ans\) into a string and return it.
The time complexity is \(O(n \times m)\), and the space complexity is \(O(n + m)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |