Skip to content

Commit 11a07ef

Browse files
committed
0936 added.
1 parent 7fdc08e commit 11a07ef

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(D)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(D ${SOURCE_FILES})
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/// Source : https://leetcode.com/problems/stamping-the-sequence/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-11-03
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Reverse Simulation
12+
/// Time Complexity: O(len(target) * len(stamp))
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
vector<int> movesToStamp(string stamp, string target) {
17+
18+
if(target.size() < stamp.size())
19+
return {};
20+
21+
if(target.size() == stamp.size()){
22+
if(stamp == target)
23+
return {0};
24+
return {};
25+
}
26+
27+
vector<int> res;
28+
while(!allQ(target, 0, target.size())){
29+
30+
int pos = possibleStamp(target, stamp);
31+
if(pos == -1)
32+
return {};
33+
34+
res.push_back(pos);
35+
for(int i = 0; i < stamp.size(); i ++)
36+
target[pos + i] = '?';
37+
}
38+
reverse(res.begin(), res.end());
39+
return res;
40+
}
41+
42+
private:
43+
bool allQ(const string& s, int start, int end){
44+
for(int i = start; i < end; i ++)
45+
if(s[i] != '?')
46+
return false;
47+
return true;
48+
}
49+
50+
int possibleStamp(const string& s, const string& stamp){
51+
52+
for(int i = 0; i <= s.size() - stamp.size(); i ++)
53+
if(!allQ(s, i, i + stamp.size()) && ok(s, i, stamp))
54+
return i;
55+
return -1;
56+
}
57+
58+
bool ok(const string& s, int start, const string& stamp){
59+
60+
for(int i = 0; i < stamp.size(); i ++)
61+
if(s[start + i] != '?' && s[start + i] != stamp[i])
62+
return false;
63+
return true;
64+
}
65+
};
66+
67+
68+
void print_vec(const vector<int>& vec){
69+
for(int e: vec)
70+
cout << e << " ";
71+
cout << endl;
72+
}
73+
74+
int main() {
75+
76+
string stamp1 = "abc", target1 = "ababc";
77+
print_vec(Solution().movesToStamp(stamp1, target1));
78+
// [0, 2]
79+
80+
string stamp2 = "abca", target2 = "aabcaca";
81+
print_vec(Solution().movesToStamp(stamp2, target2));
82+
// [3, 0, 1]
83+
84+
return 0;
85+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
588588
| 933 | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [solution](https://leetcode.com/problems/number-of-recent-calls/solution/) | [C++](0933-Number-of-Recent-Calls/cpp-0933/) | | |
589589
| 934 | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [solution](https://leetcode.com/problems/shortest-bridge/solution/) | [C++](0934-Shortest-Bridge/cpp-0934/) | | |
590590
| 935 | [Knight Dialer](https://leetcode.com/problems/knight-dialer/) | [solution](https://leetcode.com/problems/knight-dialer/solution/) | [C++](0935-Knight-Dialer/cpp-0935/) | | |
591+
| 936 | [Stamping The Sequence](https://leetcode.com/problems/stamping-the-sequence/) | [solution](https://leetcode.com/problems/stamping-the-sequence/solution/) | [C++](0936-Stamping-The-Sequence/cpp-0936/) | | |
591592
| | | | | | |

0 commit comments

Comments
 (0)