Stickers to Spell Word - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
stickers = ["with","example","science"], target = "thehat"
›
Output:
3
💡 Note:
We can use 2 "with" stickers (providing w,i,t,h each twice) and 1 "example" sticker (providing e,x,a,m,p,l,e) to get all letters: t,h,e,h,a,t requires 3 stickers total
Example 2 — Impossible Case
$
Input:
stickers = ["notice","possible"], target = "basicxyz"
›
Output:
-1
💡 Note:
The target contains 'x', 'y', 'z' which are not available in any sticker, making it impossible to spell
Example 3 — Single Sticker
$
Input:
stickers = ["these","guess","about"], target = "test"
›
Output:
2
💡 Note:
Use "these" (t,h,e,s,e) and "guess" (g,u,e,s,s) to get t,e,s,t - need 2 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
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given stickers and target word
2
Process
Find minimum stickers needed
3
Output
Return minimum count or -1
Key Takeaway
🎯 Key Insight: Use dynamic programming with character frequency states and memoization to find optimal solution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code