Stickers to Spell Word

HardStringGreedyMath

Description

Given n types of stickers (each with lowercase letters) and a target string, return the minimum number of stickers needed to spell target, or -1 if impossible.

Examples

Input:stickers = ["with","example","science"], target = "thehat"
Output:3
Explanation:

Need t,h,e,h,a,t. 'with' gives t,h; use twice for t,h,t,h. 'example' gives e,a. Total: 3 stickers.

Input:stickers = ["notice", "possible"], target = "basicstep"
Output:-1
Explanation:

'basicstep' needs 'b' which is in neither sticker. Missing required letter makes it impossible.

Input:stickers = ["control", "heart", "madle"], target = "mother"
Output:3
Explanation:

'mother' needs m,o,t,h,e,r. 'madle'->m, 'control'->o,t, 'heart'->h,e,r. One of each = 3 stickers.

Constraints

  • n == stickers.length
  • 1 ≤ n ≤ 50
  • 1 ≤ stickers[i].length ≤ 10

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!