Skip to content

Commit 0192ba6

Browse files
committed
day 31
1 parent f053ee6 commit 0192ba6

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Stamping The Sequence
3+
=====================
4+
5+
You want to form a target string of lowercase letters.
6+
7+
At the beginning, your sequence is target.length '?' marks. You also have a stamp of lowercase letters.
8+
9+
On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp. You can make up to 10 * target.length turns.
10+
11+
For example, if the initial sequence is "?????", and your stamp is "abc", then you may make "abc??", "?abc?", "??abc" in the first turn. (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)
12+
13+
If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn. If the sequence is not possible to stamp, return an empty array.
14+
15+
For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc".
16+
17+
Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length moves. Any answers specifying more than this number of moves will not be accepted.
18+
19+
Example 1:
20+
Input: stamp = "abc", target = "ababc"
21+
Output: [0,2]
22+
([1,0,2] would also be accepted as an answer, as well as some other answers.)
23+
24+
Example 2:
25+
Input: stamp = "abca", target = "aabcaca"
26+
Output: [3,0,1]
27+
28+
Note:
29+
1 <= stamp.length <= target.length <= 1000
30+
stamp and target only contain lowercase letters.
31+
*/
32+
33+
class Solution
34+
{
35+
public:
36+
bool change(int &i, string &s, string &t, vector<int> &res)
37+
{
38+
int m = s.size(), n = t.size();
39+
bool ans = false;
40+
41+
for (int j = 0; j < m; ++j)
42+
{
43+
if (t[i + j] == '?')
44+
continue;
45+
if (t[i + j] != s[j])
46+
return false;
47+
ans = true;
48+
}
49+
50+
if (ans)
51+
{
52+
for (int j = i; j < i + m; ++j)
53+
t[j] = '?';
54+
res.push_back(i);
55+
}
56+
57+
return ans;
58+
}
59+
60+
vector<int> movesToStamp(string s, string t)
61+
{
62+
int m = s.size(), n = t.size();
63+
bool changed = true;
64+
vector<int> ans;
65+
66+
while (changed)
67+
{
68+
changed = false;
69+
for (int i = 0; i < n - m + 1; ++i)
70+
{
71+
changed = (changed || change(i, s, t, ans));
72+
}
73+
}
74+
75+
bool ifStrReset = true;
76+
for (auto &i : t)
77+
if (i != '?')
78+
return {};
79+
80+
reverse(ans.begin(), ans.end());
81+
return ans;
82+
}
83+
};

Leetcode Daily Challenge/March-2021/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
| 27. | [Palindromic Substrings](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/591/week-4-march-22nd-march-28th/3686/) | [cpp](./27.%20Palindromic%20Substrings.cpp) |
3232
| 28. | [Reconstruct Original Digits from English](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/591/week-4-march-22nd-march-28th/3687/) | [cpp](./28.%20Reconstruct%20Original%20Digits%20from%20English.cpp) |
3333
| 29. | [Flip Binary Tree To Match Preorder Traversal](https://leetcode.com/explore/featured/card/march-leetcoding-challenge-2021/592/week-5-march-29th-march-31st/3689/) | [cpp](./29.%20Flip%20Binary%20Tree%20To%20Match%20Preorder%20Traversal.cpp) |
34-
| 30. | [Russian Doll Envelopes](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/592/week-5-march-29th-march-31st/3690/) | [cpp](./30.%20Russian%20Doll%20Envelopes.cpp) |
34+
| 30. | [Russian Doll Envelopes](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/592/week-5-march-29th-march-31st/3690/) | [cpp](./30.%20Russian%20Doll%20Envelopes.cpp) |
35+
| 31. | [Stamping The Sequence](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/592/week-5-march-29th-march-31st/3691/) | [cpp](./31.%20Stamping%20The%20Sequence.cpp) |

0 commit comments

Comments
 (0)