You are given two strings stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'.
In one turn, you can place stamp over s and replace every letter in the s with the corresponding letter from stamp.
For example, if stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can:
place stamp at index 0 of s to obtain "abc??",
place stamp at index 1 of s to obtain "?abc?", or
place stamp at index 2 of s to obtain "??abc".
Note that stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot place stamp at index 3 of s).
We want to convert s to target using at most10 * target.length turns.
Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain target from s within 10 * target.length turns, return an empty array.
Input: stamp ="abc", target ="ababc"Output: [0,2]Explanation: Initially s ="?????".- Place stamp at index 0 to get "abc??".- Place stamp at index 2 to get "ababc".[1,0,2] would also be accepted as an answer, as well as some other answers.
Input: stamp ="abca", target ="aabcaca"Output: [3,0,1]Explanation: Initially s ="???????".- Place stamp at index 3 to get "???abca".- Place stamp at index 0 to get "abcabca".- Place stamp at index 1 to get "aabcaca".
Instead of building the target from all ‘?’, we reverse the process: start from the target and repeatedly “unstamp” it by replacing stamp substrings with ‘?’. This way, we can greedily find the last stamp positions and work backwards, ensuring we do not exceed the allowed number of moves.
classSolution {
public: vector<int> movesToStamp(string stamp, string target) {
int n = target.size(), m = stamp.size();
vector<int> ans;
bool changed = true;
vector<char> t(target.begin(), target.end());
while (changed) {
changed = false;
for (int i =0; i <= n - m; ++i) {
bool can = false;
for (int j =0; j < m; ++j) {
if (t[i+j] !='?'&& t[i+j] != stamp[j]) break;
if (stamp[j] == t[i+j]) can = true;
if (j == m-1) {
if (can) {
for (int k =0; k < m; ++k) t[i+k] ='?';
ans.push_back(i);
changed = true;
}
}
}
}
}
for (char c : t) if (c !='?') return {};
reverse(ans.begin(), ans.end());
return ans;
}
};
classSolution {
publicint[]movesToStamp(String stamp, String target) {
int n = target.length(), m = stamp.length();
char[] t = target.toCharArray();
List<Integer> ans =new ArrayList<>();
boolean changed =true;
while (changed) {
changed =false;
for (int i = 0; i <= n - m; ++i) {
boolean can =false;
for (int j = 0; j < m; ++j) {
if (t[i+j]!='?'&& t[i+j]!= stamp.charAt(j)) break;
if (stamp.charAt(j) == t[i+j]) can =true;
if (j == m-1) {
if (can) {
for (int k = 0; k < m; ++k) t[i+k]='?';
ans.add(i);
changed =true;
}
}
}
}
}
for (char c : t) if (c !='?') returnnewint[0];
Collections.reverse(ans);
return ans.stream().mapToInt(i->i).toArray();
}
}
classSolution {
funmovesToStamp(stamp: String, target: String): IntArray {
val n = target.length
val m = stamp.length
val t = target.toCharArray()
val ans = mutableListOf<Int>()
var changed = truewhile (changed) {
changed = falsefor (i in0..n-m) {
var can = falsefor (j in0 until m) {
if (t[i+j] !='?'&& t[i+j] != stamp[j]) breakif (stamp[j] == t[i+j]) can = trueif (j == m-1) {
if (can) {
for (k in0 until m) t[i+k] = '?' ans.add(i)
changed = true }
}
}
}
}
if (t.any { it!='?' }) return intArrayOf()
return ans.reversed().toIntArray()
}
}
defmovesToStamp(stamp: str, target: str) -> list[int]:
n, m = len(target), len(stamp)
t = list(target)
ans = []
changed =Truewhile changed:
changed =Falsefor i in range(n-m+1):
can =Falsefor j in range(m):
if t[i+j] !='?'and t[i+j] != stamp[j]:
breakif stamp[j] == t[i+j]:
can =Trueif j == m-1:
if can:
for k in range(m):
t[i+k] ='?' ans.append(i)
changed =Trueif any(c !='?'for c in t):
return []
return ans[::-1]
impl Solution {
pubfnmoves_to_stamp(stamp: String, target: String) -> Vec<i32> {
let n = target.len();
let m = stamp.len();
letmut t: Vec<char>= target.chars().collect();
letmut ans = Vec::new();
letmut changed =true;
while changed {
changed =false;
for i in0..=n-m {
letmut can =false;
for j in0..m {
if t[i+j] !='?'&& t[i+j] != stamp.chars().nth(j).unwrap() { break; }
if stamp.chars().nth(j).unwrap() == t[i+j] { can =true; }
if j == m-1 {
if can {
for k in0..m { t[i+k] ='?'; }
ans.push(i asi32);
changed =true;
}
}
}
}
}
if t.iter().any(|&c| c !='?') { returnvec![]; }
ans.reverse();
ans
}
}