forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
85 lines (65 loc) · 1.94 KB
/
main.cpp
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
/// Source : https://leetcode.com/problems/stamping-the-sequence/
/// Author : liuyubobobo
/// Time : 2018-11-03
#include <iostream>
#include <vector>
using namespace std;
/// Reverse Simulation
/// Time Complexity: O(len(target) * len(stamp))
/// Space Complexity: O(1)
class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
if(target.size() < stamp.size())
return {};
if(target.size() == stamp.size()){
if(stamp == target)
return {0};
return {};
}
vector<int> res;
while(!allQ(target, 0, target.size())){
int pos = possibleStamp(target, stamp);
if(pos == -1)
return {};
res.push_back(pos);
for(int i = 0; i < stamp.size(); i ++)
target[pos + i] = '?';
}
reverse(res.begin(), res.end());
return res;
}
private:
bool allQ(const string& s, int start, int end){
for(int i = start; i < end; i ++)
if(s[i] != '?')
return false;
return true;
}
int possibleStamp(const string& s, const string& stamp){
for(int i = 0; i <= s.size() - stamp.size(); i ++)
if(!allQ(s, i, i + stamp.size()) && ok(s, i, stamp))
return i;
return -1;
}
bool ok(const string& s, int start, const string& stamp){
for(int i = 0; i < stamp.size(); i ++)
if(s[start + i] != '?' && s[start + i] != stamp[i])
return false;
return true;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
string stamp1 = "abc", target1 = "ababc";
print_vec(Solution().movesToStamp(stamp1, target1));
// [0, 2]
string stamp2 = "abca", target2 = "aabcaca";
print_vec(Solution().movesToStamp(stamp2, target2));
// [3, 0, 1]
return 0;
}