1
var movesToStamp = function (stamp, target) {
2
let res = [];
3
let total_stamp = 0,
4
turn_stamp = -1;
5
while (turn_stamp) {
6
turn_stamp = 0;
7
for (let sz = stamp.length; sz > 0; --sz)
8
for (let i = 0; i <= stamp.length - sz; ++i) {
9
let new_stamp =
10
"*".repeat(i) +
11
stamp.slice(i, sz + i) +
12
"*".repeat(stamp.length - sz - i); // preparing 'abc', '*bc', 'ab*', 'a**' etc
13
let pos = target.indexOf(new_stamp); // finding position of prepared new_stamp in target
14
while (pos != -1) {
15
// if position is found
16
res.push(pos); // then push position into response array
17
turn_stamp += sz; // sz is size on new_stamp without '*'. so for 'abc' sz equals 3, for 'a**' sz equals 1
18
target =
19
target.slice(0, pos) +
20
"*".repeat(stamp.length) +
21
target.slice(pos + stamp.length); // insert new_stamp into target
22
pos = target.indexOf(new_stamp); // try to find another place in target where we can insert new_stamp
23
}
24
}
25
total_stamp += turn_stamp;
26
}
27
res.reverse();
28
return total_stamp == target.length ? res : []; // we check if all characters converted to '*'
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0