Find the fewest reusable word stickers needed to spell a target string

Quick Overview

Given word stickers that can each be reused any number of times, find the fewest stickers whose cut-out letters spell a target string, or report that it cannot be done. It tests modeling the letters still needed as a search state, pruning the choices at each step, and avoiding a greedy pick that fails.

Find the fewest reusable word stickers needed to spell a target string

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

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 ```python 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 ```text 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. ```text Input: stickers = ["notice","possible"], target = "basicbasic" Output: -1 ``` No sticker contains the letter a, so "basicbasic" cannot be spelled. ```text 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.

Overview: Given word stickers that can each be reused any number of times, find the fewest stickers whose cut-out letters spell a target string, or report that it cannot be done. It tests modeling the letters still needed as a search state, pruning the choices at each step, and avoiding a greedy pick that fails.

Meta logo
Meta
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

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.