Problem · Dynamic Programming
Minimum Stickers to Form a Target String
Learn this problemProblem 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
-1when the target cannot be formed.
Function
minStickers(stickers: String[], target: String) → intExamples
Example 1
stickers = ["with","example","science"]target = "thehat"return = 3Two 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 = -1No 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.