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: 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.
We need to construct the lexicographically smallest string of length n+m-1 that satisfies the overlap constraints from str1 and str2. For each position, if str1[i] == 'T', the substring must match str2; if str1[i] == 'F', it must not. We can use backtracking with pruning, always trying the smallest possible character at each position, and backtrack if a constraint is violated.
classSolution:
defsmallestString(self, str1: str, str2: str) -> str:
n, m = len(str1), len(str2)
L = n + m -1 res = [''] * L
defbacktrack(pos: int) -> bool:
if pos == L:
for i in range(n):
sub =''.join(res[i:i+m])
if str1[i] =='T'and sub != str2:
returnFalseif str1[i] =='F'and sub == str2:
returnFalsereturnTruefor c in map(chr, range(ord('a'), ord('z')+1)):
res[pos] = c
valid =True i = pos - m +1if0<= i < n:
sub =''.join(res[i:i+m])
if''notin sub:
if str1[i] =='T'and sub != str2:
valid =Falseif str1[i] =='F'and sub == str2:
valid =Falseif valid and backtrack(pos+1):
returnTruereturnFalseif backtrack(0):
return''.join(res)
return''
classSolution {
public: string smallestString(string str1, string str2) {
int n = str1.size(), m = str2.size(), L = n + m -1;
string res(L, 'a');
function<bool(int)> dfs = [&](int pos) {
if (pos == L) {
for (int i =0; i < n; ++i) {
string sub = res.substr(i, m);
if (str1[i] =='T'&& sub != str2) return false;
if (str1[i] =='F'&& sub == str2) return false;
}
return true;
}
for (char c ='a'; c <='z'; ++c) {
res[pos] = c;
bool valid = true;
int i = pos - m +1;
if (i >=0&& i < n) {
string sub = res.substr(i, m);
if (sub.find(' ') == string::npos) {
if (str1[i] =='T'&& sub != str2) valid = false;
if (str1[i] =='F'&& sub == str2) valid = false;
}
}
if (valid && dfs(pos+1)) return true;
}
return false;
};
if (dfs(0)) return res;
return"";
}
};
classSolution {
public String smallestString(String str1, String str2) {
int n = str1.length(), m = str2.length(), L = n + m - 1;
char[] res =newchar[L];
Arrays.fill(res, 'a');
if (dfs(0, res, str1, str2, n, m, L)) returnnew String(res);
return"";
}
privatebooleandfs(int pos, char[] res, String str1, String str2, int n, int m, int L) {
if (pos == L) {
for (int i = 0; i < n; ++i) {
String sub =new String(res, i, m);
if (str1.charAt(i) =='T'&&!sub.equals(str2)) returnfalse;
if (str1.charAt(i) =='F'&& sub.equals(str2)) returnfalse;
}
returntrue;
}
for (char c ='a'; c <='z'; ++c) {
res[pos]= c;
boolean valid =true;
int i = pos - m + 1;
if (i >= 0 && i < n) {
String sub =new String(res, i, m);
if (new String(sub).indexOf('\0') ==-1) {
if (str1.charAt(i) =='T'&&!sub.equals(str2)) valid =false;
if (str1.charAt(i) =='F'&& sub.equals(str2)) valid =false;
}
}
if (valid && dfs(pos+1, res, str1, str2, n, m, L)) returntrue;
}
returnfalse;
}
}
impl Solution {
pubfnsmallest_string(str1: String, str2: String) -> String {
let n = str1.len();
let m = str2.len();
let L = n + m -1;
letmut res =vec![b'a'; L];
fndfs(pos: usize, res: &mut Vec<u8>, str1: &[u8], str2: &[u8], n: usize, m: usize, L: usize) -> bool {
if pos == L {
for i in0..n {
let sub =&res[i..i+m];
if str1[i] ==b'T'&& sub != str2 { returnfalse; }
if str1[i] ==b'F'&& sub == str2 { returnfalse; }
}
returntrue;
}
for c inb'a'..=b'z' {
res[pos] = c;
letmut valid =true;
let i = pos asisize- m asisize+1;
if i >=0&& i < n asisize {
let sub =&res[i asusize..i asusize+ m];
if str1[i asusize] ==b'T'&& sub != str2 { valid =false; }
if str1[i asusize] ==b'F'&& sub == str2 { valid =false; }
}
if valid && dfs(pos+1, res, str1, str2, n, m, L) { returntrue; }
}
false }
if dfs(0, &mut res, str1.as_bytes(), str2.as_bytes(), n, m, L) {
String::from_utf8(res).unwrap()
} else {
String::new()
}
}
}