Skip to content

Commit b875116

Browse files
authored
Update 691-stickers-to-spell-word.js
1 parent 2baf088 commit b875116

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

691-stickers-to-spell-word.js

+34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
/**
2+
* @param {string[]} stickers
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
const minStickers = function (stickers, target) {
7+
const n = stickers.length
8+
const m = target.length
9+
const limit = 1 << m
10+
const dp = Array(limit).fill(Infinity)
11+
dp[0] = 0
12+
for (let i = 0; i < limit; i++) {
13+
if (dp[i] === Infinity) continue
14+
for (const sticker of stickers) {
15+
let stat = i
16+
for (const ch of sticker) {
17+
for (let j = 0; j < m; j++) {
18+
if (target[j] === ch && ((stat >> j) & 1) === 0) {
19+
stat |= (1 << j)
20+
break
21+
}
22+
}
23+
}
24+
dp[stat] = Math.min(dp[stat], dp[i] + 1)
25+
}
26+
}
27+
28+
return dp[limit - 1] === Infinity ? -1 : dp[limit - 1]
29+
}
30+
31+
32+
// another
33+
34+
135
/**
236
* @param {string[]} stickers
337
* @param {string} target

0 commit comments

Comments
 (0)