We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2baf088 commit b875116Copy full SHA for b875116
691-stickers-to-spell-word.js
@@ -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
35
/**
36
* @param {string[]} stickers
37
* @param {string} target
0 commit comments