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.
Return the minimum number of stickers that you need to spell out target. If the task is impossible, return -1.
Function Signature
def min_stickers(stickers: list[str], target: str) -> int:
Rules
-
Each letter cut from a sticker fills one position of
target
. Letters of a sticker that you do not need are thrown away.
-
Every copy of a sticker you use counts as one sticker, however many of its letters end up in
target
.
Constraints
-
n == len(stickers)
-
1 <= n <= 50
-
1 <= len(stickers[i]) <= 10
-
1 <= len(target) <= 15
-
stickers[i]
and
target
consist of lowercase English letters.
Examples
Input: stickers = ["with","example","science"], target = "thehat"
Output: 3
Two copies of "with" supply t, h, t, h and one copy of "example" supplies e and a, which spells "thehat". Fewer is impossible: each "with" has a single t and no other sticker has a t, so two copies of "with" are needed, and only "example" has an a.
Input: stickers = ["notice","possible"], target = "basicbasic"
Output: -1
No sticker contains the letter a, so "basicbasic" cannot be spelled.
Input: stickers = ["abcd","abe","cdf"], target = "abcdef"
Output: 2
"abe" and "cdf" together hold all six letters, and no single sticker does. Starting with "abcd", the sticker that covers the most letters, leaves e and f, which then take two more stickers for a total of three.