You want to form a `target` string of lowercase letters.
At the beginning, your sequence is target.length
'?'
marks. You also have a stamp
of lowercase letters.
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.
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.)
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.
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".
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.
Example 1:
Input: stamp = "abc", target = "ababc"
Output: [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)
Example 2:
Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]
Note:
1 <= stamp.length <= target.length <= 1000
stamp
andtarget
only contain lowercase letters.
這道題給了一個目標字符串 target,還有一個印戳字符串 stamp,現在有一個長度跟 target 一樣的一排問號,每次可以在某個位置蓋上印戳,新的印戳將會覆蓋之前的字符,不論是問號還是其他字符,現在讓找出所有蓋印戳的位置,使得剛好可以蓋出給定的字符串 target。這道題乍一看感覺還挺難下手的,畢竟一排問號,我們怎么知道該從哪里開始蓋,但是如果換一個方向,假如給的是 target 字符串,每次蓋印章,將對應的位置變成星號,只要將 target 中所有的字符蓋成星號,最終再把蓋印章的順序翻轉一下,就是題目中所求了。這里參考的是 [votrubac 大神的帖子](https://leetcode.com/problems/stamping-the-sequence/discuss/189576/C%2B%2B-simple-greedy),比如 target="aabccbc",stamp="abc",那么首先肯定是在 target 中找整個的 abc,可以找到,從位置1出開始蓋,target 變為 a\*\*\*cbc,同時標記此時已經蓋了3個字母,加入到 total 變量中。然后繼續找 abc,沒有的話,就需要改變印戳了,開始往里面加星號,首先加一個星號,加的位置有三個,分別是 ab\*, a\*c, \*bc,發現這三種都無法匹配,於是開始加兩個星號,就有 a\*\*,\*\*c,其中 a\*\* 可以成功匹配,起始位置為0,total 加1,於是 target 變為 ****cbc,然后發現此時 \*\*c 也可以成功匹配,起始位置為2,total 加1,target 變為 *****bc。現在並不需要給 stamp 中加三個星號,這樣沒有意義,要做的是再從開頭找一遍,此時發現 \*bc 可以匹配,起始位置為4,total 加2,到現在位置 target 完全變為星號,當無法進行蓋印戳的時候,就退出循環,需要一個 isStamped 的變量來標記一下是否進行了戳印。循環退出后要將 res 數組翻轉一下,同時還要看 total 是否等於 target 的長度,只有相等了,才說明每個字母都被戳印了,否則返回空集,參見代碼如下:
class Solution {
public:
vector<int> movesToStamp(string stamp, string target) {
vector<int> res;
int n = stamp.size(), total = 0;
while (true) {
bool isStamped = false;
for (int size = n; size > 0; --size) {
for (int i = 0; i <= n - size; ++i) {
string t = string(i, '*') + stamp.substr(i, size) + string(n - size - i, '*');
auto pos = target.find(t);
while (pos != string::npos) {
res.push_back(pos);
isStamped = true;
total += size;
fill(begin(target) + pos, begin(target) + pos + n, '*');
pos = target.find(t);
}
}
}
if (!isStamped) break;
}
reverse(res.begin(), res.end());
return total == target.size() ? res : vector<int>();
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/936
參考資料:
https://leetcode.com/problems/stamping-the-sequence/
https://leetcode.com/problems/stamping-the-sequence/discuss/189576/C%2B%2B-simple-greedy
[LeetCode All in One 題目講解匯總(持續更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)