-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
227 lines (202 loc) · 6.54 KB
/
main.cpp
File metadata and controls
227 lines (202 loc) · 6.54 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Source: https://leetcode.com/problems/lexicographically-smallest-generated-string
// Title: Lexicographically Smallest Generated String
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 `""`.
//
// **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 <= 10^4`
// - `1 <= m == str2.length <= 500`
// - `str1` consists only of `'T'` or `'F'`.
// - `str2` consists only of lowercase English characters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <bitset>
#include <vector>
using namespace std;
// Greedy + Bit Mask
//
// We initialize the output string `s` with all letter `a`.
// Fill the `s` by str1[i]=T and mark these position as fixed.
//
// Now we need to fill the free slots.
// Focus on each s[i:i+m) with str1[i] = F.
// If s[i:i+m) != str2, then it is already done.
// Otherwise, since we want s to be lexicographically smallest,
// we will always change the last free slot in s[i:i+m).
// However, we don't want to break previous "done" F.
//
// To avoid that, precompute the last free slot of each F.
// Now we loop through the free slots.
// Pick all F that corresponds to this slot as the last,
// check this slot is important for the constraint (i.e. other position are matched).
// If so, we use a bitmask to track which letter is not allowed for this slot.
// After checking all F for this slot, we pick the smallest allowed letter.
class Solution {
struct Mask : bitset<26> {
int low_zero() const { // find lowest zero
return countr_one(to_ulong());
}
};
using Bool = unsigned char;
public:
string generateString(const string& str1, const string& str2) {
const int n = str1.size(), m = str2.size();
const int l = n + m - 1;
auto s = string(l, 'a'); // fill with all a
auto fixed = vector<Bool>(l, false);
auto slotToIdxs = vector<vector<int>>(l); // free slot -> F index
// Fill by T
for (int i = 0; i < n; ++i) {
if (str1[i] != 'T') continue;
for (int j = i; j < i + m; ++j) {
if (fixed[j] && s[j] != str2[j - i]) return ""; // invalid
s[j] = str2[j - i];
fixed[j] = true;
}
}
// Find free slot
for (int i = 0; i < n; ++i) {
if (str1[i] != 'F') continue;
// Check done
bool done = false;
for (int j = i; j < i + m; ++j) {
if (s[j] != str2[j - i]) {
done = true;
break;
}
}
if (done) continue;
// Find last free slot
int lastSlot = -1;
for (int j = i + m - 1; j >= i; --j) {
if (!fixed[j]) {
lastSlot = j;
break;
}
}
if (lastSlot == -1) return ""; // invalid
slotToIdxs[lastSlot].push_back(i);
}
// Loop for slots
for (int slot = 0; slot < l; ++slot) {
if (fixed[slot]) continue;
Mask disallowed; // disallowed letters
for (int i : slotToIdxs[slot]) {
// Check done
bool done = false;
for (int j = i; j < i + m; ++j) {
if (j == slot) continue;
if (s[j] != str2[j - i]) {
done = true;
break;
}
}
if (done) continue;
// Exclude letter
disallowed.set(str2[slot - i] - 'a');
}
if (disallowed.all()) return ""; // no allowed letter
s[slot] = disallowed.low_zero() + 'a';
}
return s;
}
};
// Greedy + Insight
//
// We "notice" that the free slot is either `a` or `b`.
// (why????????????????)
//
// We initialize the output string `s` with all letter `a`.
// Next fill the `s` by str1[i]=T.
// Next loop for each str1[i]=F.
// If s[i:i+m) != str2, then it is already done.
// Otherwise, find the last free slot and change it to `b`.
class Solution2 {
using Bool = unsigned char;
public:
string generateString(const string& str1, const string& str2) {
const int n = str1.size(), m = str2.size();
const int l = n + m - 1;
auto s = string(l, 'a'); // fill with all a
auto fixed = vector<Bool>(l, false);
// Fill by T
for (int i = 0; i < n; ++i) {
if (str1[i] != 'T') continue;
for (int j = i; j < i + m; ++j) {
if (fixed[j] && s[j] != str2[j - i]) return ""; // invalid
s[j] = str2[j - i];
fixed[j] = true;
}
}
// Greedy
for (int i = 0; i < n; ++i) {
if (str1[i] != 'F') continue;
// check if is already done
bool flag = false;
for (int j = i; j < i + m; ++j) {
if (s[j] != str2[j - i]) {
flag = true;
break;
}
}
if (flag) continue;
// Find last free slot
int idx = -1;
for (int j = i + m - 1; j >= i; --j) {
if (!fixed[j]) {
idx = j;
break;
}
}
if (idx == -1) return "";
s[idx] = 'b'; // change to b
// fixed[idx] = true; // don't need this (why????????????????)
}
return s;
}
};