-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path936.cpp
More file actions
40 lines (38 loc) · 1.14 KB
/
936.cpp
File metadata and controls
40 lines (38 loc) · 1.14 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
class Solution {
public:
int replace(string& stamp, string& target, int index) {
int n = stamp.size();
int cnt = n;
for (int i = 0; i < n; ++i) {
if (target[index + i] == '*') {
cnt--;
}
else if (target[index + i] != stamp[i]) {
return 0;
}
}
fill(target.begin() + index, target.begin() + index + n, '*');
return cnt;
}
vector<int> movesToStamp(string stamp, string target) {
int n = target.size();
int m = stamp.size();
vector<bool> stamped(n, false);
int required = n;
vector<int> res;
while (required) {
bool hasReplaced = false;
for (int i = 0; i + m <= n; ++i) {
if (stamped[i]) continue;
int l = replace(stamp, target, i);
if (l == 0) continue;
res.push_back(i);
hasReplaced = true;
required -= l;
}
if (!hasReplaced) return {};
}
reverse(res.begin(), res.end());
return res;
}
};