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
Stickers to Spell Word: Find Minimum StickersAvailable Stickers:"with""example""science"Target Word"thehat"Find minimum stickers to get all letters: t,h,e,h,a,t"with" → w,i,t,h"example" → e,a"with" → remaining tNeed: t(2), h(2), e(1), a(1) → Use 3 stickersOutput: 3
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
Asked in
Google 25 Amazon 20 Microsoft 15 Facebook 18
28.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen