forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
35 lines (35 loc) · 1.06 KB
/
s1.cpp
File metadata and controls
35 lines (35 loc) · 1.06 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
// OJ: https://leetcode.com/problems/stamping-the-sequence/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(1)
class Solution {
private:
int findStamp(string &stamp, string &target) {
for (int i = 0; i <= target.size() - stamp.size(); ++i) {
int j = 0, cnt = 0;
for (; j < stamp.size(); ++j) {
if (target[i + j] == '?') continue;
if (target[i + j] != stamp[j]) break;
++cnt;
}
if (cnt && j == stamp.size()) return i;
}
return -1;
}
public:
vector<int> movesToStamp(string stamp, string target) {
vector<int> ans;
int cnt = target.size();
while (cnt) {
int start = findStamp(stamp, target);
if (start == -1) return {};
ans.push_back(start);
for (int i = 0; i < stamp.size(); ++i) {
if (target[start + i] != '?') --cnt;
target[start + i] = '?';
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};