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 string target 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.
Return the minimum number of stickers that you need to spell out target. If the task is impossible, return -1.
Note: In all test cases, all words were chosen randomly from the 1000 most common US English words, and target was chosen as a concatenation of two random words.
Example 1:
Input: stickers = [“with”,“example”,“science”], target = “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: stickers = [“notice”,“possible”], target = “basicbasic”
Output: -1
Explanation:
We cannot form the target “basicbasic” from cutting letters from the given stickers.
Constraints:
- n == stickers.length
- 1 <= n <= 50
- 1 <= stickers[i].length <= 10
- 1 <= target.length <= 15
- stickers[i] and target consist of lowercase English letters.
From: LeetCode
Link: 691. Stickers to Spell Word
Solution:
Ideas:
-
Bitmask is used to represent which characters in target are still needed.
-
dfs(mask) tries to reduce the characters left using each sticker and recurses with the new mask.
-
Memoization (dp[mask]) avoids recomputation.
Code:
#define MAXN 50
#define MAXLEN 15
int dp[1 << MAXLEN]; // dp[mask] = min number of stickers to finish mask
int minStickers(char** stickers, int stickersSize, char* target) {
int targetLen = strlen(target);
int targetFreq[MAXLEN][26];
// Build target frequency positions
for (int i = 0; i < targetLen; ++i) {
for (int j = 0; j < 26; ++j) {
targetFreq[i][j] = 0;
}
}
for (int i = 0; i < targetLen; ++i) {
targetFreq[i][target[i] - 'a']++;
}
// Preprocess sticker frequencies
int stickerFreq[MAXN][26];
for (int i = 0; i < stickersSize; ++i) {
memset(stickerFreq[i], 0, sizeof(stickerFreq[i]));
for (int j = 0; stickers[i][j]; ++j) {
stickerFreq[i][stickers[i][j] - 'a']++;
}
}
for (int i = 0; i < (1 << targetLen); ++i) {
dp[i] = -1;
}
dp[0] = 0; // No characters needed
int dfs(int mask) {
if (dp[mask] != -1) return dp[mask];
int res = INT_MAX;
for (int i = 0; i < stickersSize; ++i) {
// Try using sticker i
int newMask = mask;
int stickerCount[26];
memcpy(stickerCount, stickerFreq[i], sizeof(stickerCount));
for (int j = 0; j < targetLen; ++j) {
if ((mask >> j) & 1) {
int ch = target[j] - 'a';
if (stickerCount[ch]) {
stickerCount[ch]--;
newMask ^= (1 << j); // mark character as done
}
}
}
if (newMask < mask) { // sticker helped
int sub = dfs(newMask);
if (sub != INT_MAX)
res = fmin(res, 1 + sub);
}
}
dp[mask] = res;
return res;
}
int result = dfs((1 << targetLen) - 1);
return result == INT_MAX ? -1 : result;
}