Minimum Stickers to Form Target

You are given an array of strings stickers, where each string represents a sticker containing lowercase English letters.

You want to form the string target by cutting out individual characters from the stickers and rearranging them.

Rules:

  • You may use any sticker multiple times.
  • You have an infinite supply of every sticker.
  • Each character from a sticker can be used only once per copy of that sticker.

Return the minimum number of stickers required to form target.

If it is not possible to form target, return -1.

Example 1:

Input: stickers = ["ab","bc","ca"], target = "aabbcc"

Output: 3

Explanation:

One valid way is:

  • Use "ab" once to get a, b
  • Use "ca" once to get a, c
  • Use "bc" once to get b, c

Now all characters of "aabbcc" can be formed.

It is not possible to do this using fewer than 3 stickers.

Example 2:

Input: stickers = ["apple","orange","banana"], target = "grape"

Output: 2

Explanation:

We can use:

  • "orange" to get g, r, a, e
  • "apple" to get p

So the target "grape" can be formed using 2 stickers.

Now Your Turn!

Pick the correct output for the given input

Input: stickers = ["these","guess","about","garden"], target = "seed"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= stickers.length <= 50
  • 1 <= stickers[i].length <= 10
  • 1 <= target.length <= 15
  • stickers[i] and target consist only of lowercase English letters.

Fun Facts

0
class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
// Your code goes here
}
};
Test Case

Input:

Stickers
Target