Problem · Dynamic Programming

Minimum Stickers to Form a Target String

Learn this problem
HardByteDance logoByteDanceFULLTIMEONSITE INTERVIEW

Problem statement

Given sticker strings and a target string, return the minimum number of stickers needed to form the target.

  • Each sticker type may be used any number of times.
  • Each occurrence of a letter on a chosen sticker may cover at most one equal, still-uncovered target position.
  • Letters on a sticker may be used in any order, and unused letters may be discarded.
  • Return -1 when the target cannot be formed.

Function

minStickers(stickers: String[], target: String) → int

Examples

Example 1

stickers = ["with","example","science"]target = "thehat"return = 3

Two copies of with and one copy of example can supply all letters of thehat, and two stickers cannot cover every required occurrence.

Example 2

stickers = ["notice","possible"]target = "basicbasic"return = -1

No sticker contains the letter a, so the target is impossible.

Constraints

  • 1 <= stickers.length <= 50.
  • 1 <= stickers[i].length <= 20.
  • 0 <= target.length <= 15.
  • Every sticker and the target contain only lowercase English letters.

More ByteDance problems

drafts saved locally
public int minStickers(String[] stickers, String target) {
    // TODO: return the minimum number of reusable stickers.
}
stickers["with","example","science"]
target"thehat"
expected3
checking account