Stickers to Spell Word

Hard Topics Company Tags

Each string in stickers describes the letters on one sticker type. You have unlimited copies of every type. Cut letters from chosen stickers and rearrange them to spell target; unused letters may be discarded. Return the fewest stickers needed, or -1 if the target cannot be formed. Each letter printed on a sticker can be used at most once from that copy.

Example 1:

Input: stickers = ["ab","bc"]
target = "abc"

Output: 2

Explanation: One ab sticker and one bc sticker supply a, b, and c, with one b left over. No single sticker contains all three required letters, so the minimum is 2.

Example 2:

Input: stickers = ["aab"]
target = "aaaab"

Output: 2

Explanation: Two copies of aab supply four a letters and two b letters. Discard one b. One copy has only two a letters, so 2 stickers are necessary.

Example 3:

Input: stickers = ["ab","bc"]
target = "ad"

Output: -1

Explanation: Neither sticker contains d. Additional copies cannot supply it, so the answer is -1.

Constraints:

  • 1 <= stickers.length <= 50
  • 1 <= stickers[i].length <= 10
  • 1 <= target.length <= 15
  • All strings contain only lowercase English letters.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.