Skip to content

Commit 0743664

Browse files
authored
Update 691-stickers-to-spell-word.js
1 parent ca8a47c commit 0743664

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

691-stickers-to-spell-word.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
/**
2+
* @param {string[]} stickers
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
const minStickers = function(stickers, target) {
7+
const n = target.length
8+
const limit = 1 << n
9+
const dp = Array(limit).fill(Infinity)
10+
dp[0] = 0
11+
for(let state = 0; state < limit; state++) {
12+
if(dp[state] === Infinity) continue
13+
for(let s of stickers) {
14+
const ns = helper(state, target, s)
15+
dp[ns] = Math.min(dp[ns], dp[state] + 1)
16+
}
17+
18+
}
19+
20+
return dp[limit - 1] === Infinity ? -1 : dp[limit - 1]
21+
22+
function helper(state, target, s) {
23+
const n = target.length
24+
for(const ch of s) {
25+
for(let i = 0; i < n; i++) {
26+
if(target[i] === ch && ((state >> i) & 1) === 0) {
27+
state |= (1 << i)
28+
break
29+
}
30+
}
31+
}
32+
33+
return state
34+
}
35+
};
36+
37+
// another
38+
39+
140
/**
241
* @param {string[]} stickers
342
* @param {string} target

0 commit comments

Comments
 (0)