# 691. Stickers to Spell Word

We are given N different types of stickers. Each sticker has a lowercase English word on it.

You would like to spell out the given `target` string by cutting individual letters from your collection of stickers and rearranging them.

You can use each sticker more than once if you want, and you have infinite quantities of each sticker.

What is the minimum number of stickers that you need to spell out the `target`? If the task is impossible, return -1.

Example 1:

Input:

``````["with", "example", "science"], "thehat"
``````

Output:

``````3
``````

Explanation:

``````We can use 2 "with" stickers, and 1 "example" sticker.
After cutting and rearrange the letters of those stickers, we can form the target "thehat".
Also, this is the minimum number of stickers necessary to form the target string.
``````

Example 2:

Input:

``````["notice", "possible"], "basicbasic"
``````

Output:

``````-1
``````

Explanation:

``````We can't form the target "basicbasic" from cutting letters from the given stickers.
``````

Note:

• `stickers` has length in the range `[1, 50]`.
• `stickers` consists of lowercase English words (without apostrophes).
• `target` has length in the range `[1, 15]`, and consists of lowercase English letters.
• In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words.
• The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average.

``````     abc   subset
0    000     ""
1    001     c
2    010     b
3    011     bc
4    100     a
5    101     ac
6    110     bb
7    111     abc
``````

stickers = [“ab”, “bc”, “ac”], target = “abc”

``````class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
int n = target.size(), m = 1 << n;
vector<int> dp(m, INT_MAX);
dp[0] = 0;
for (int i = 0; i < m; ++i) {
if (dp[i] == INT_MAX) continue;
for (string &sticker : stickers) {
int cur = i;
for (char c : sticker) {
for (int k = 0; k < n; ++k) {
if (target[k] == c && !((cur >> k) & 1)) {
cur |= 1 << k;
break;
}
}
}
dp[cur] = min(dp[cur], dp[i] + 1);
}
}
return dp[m - 1] == INT_MAX ? -1 : dp[m - 1];
}
};
``````

``````class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
int N = stickers.size();
vector<vector<int>> freq(N, vector<int>(26, 0));
unordered_map<string, int> memo;
memo[""] = 0;
for (int i = 0; i < N; ++i) {
for (char c : stickers[i]) ++freq[i][c - 'a'];
}
return helper(freq, target, memo);
}
int helper(vector<vector<int>>& freq, string target, unordered_map<string, int>& memo) {
if (memo.count(target)) return memo[target];
int res = INT_MAX, N = freq.size();
vector<int> cnt(26, 0);
for (char c : target) ++cnt[c - 'a'];
for (int i = 0; i < N; ++i) {
if (freq[i][target[0] - 'a'] == 0) continue;
string t = "";
for (int j = 0; j < 26; ++j) {
if (cnt[j] - freq[i][j] > 0) t += string(cnt[j] - freq[i][j], 'a' + j);
}
int ans = helper(freq, t, memo);
if (ans != -1) res = min(res, ans + 1);
}
memo[target] = (res == INT_MAX) ? -1 : res;
return memo[target];
}
};
``````

Ransom Note

https://leetcode.com/problems/stickers-to-spell-word/discuss/108333/Rewrite-of-contest-winner's-solution

https://leetcode.com/problems/stickers-to-spell-word/discuss/108318/C++JavaPython-DP-+-Memoization-with-optimization-29-ms-(C++)

https://leetcode.com/problems/stickers-to-spell-word/discuss/108334/Explaining-StefanPochmann's-Rewrite-of-contest-winner's-solution-and-+java

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation