Intuition

A natural answer is to exhaustively search combinations of stickers. Because the data is randomized, there are many heuristics available to us that will make this faster.

  • For all stickers, we can ignore any letters that are not in the target word.

  • When our candidate answer won't be smaller than an answer we have already found, we can stop searching this path.

  • We should try to have our exhaustive search bound the answer as soon as possible, so the effect described in the above point happens more often.

  • When a sticker dominates another, we shouldn't include the dominated sticker in our sticker collection. [Here, we say a sticker A dominates B if A.count(letter) >= B.count(letter) for all letters.]


Algorithm

Firstly, for each sticker, let's create a count of that sticker (a mapping letter -> sticker.count(letter)) that does not consider letters not in the target word. Let A be an array of these counts. Also, let's create t_count, a count of our target word.

Secondly, let's remove dominated stickers. Because dominance is a transitive relation, we only need to check if a sticker is not dominated by any other sticker once - the ones that aren't dominated are included in our collection.

We are now ready to begin our exhaustive search. A call to search(ans) denotes that we want to decide the minimum number of stickers we can used in A to satisfy the target count t_count. ans will store the currently formed answer, and best will store the current best answer.

If our current answer can't beat our current best answer, we should stop searching. Also, if there are no stickers left and our target is satisfied, we should update our answer.

Otherwise, we want to know the maximum number of this sticker we can use. For example, if this sticker is 'abb' and our target is 'aaabbbbccccc', then we could use a maximum of 3 stickers. This is the maximum of math.ceil(target.count(letter) / sticker.count(letter)), taken over all letters in sticker. Let's call this quantity used.

After, for the sticker we are currently considering, we try to use used of them, then used - 1, used - 2 and so on. The reason we do it in this order is so that we can arrive at a value for best more quickly, which will stop other branches of our exhaustive search from continuing.

The Python version of this solution showcases using collections.Counter as a way to simplify some code sections, whereas the Java solution sticks to arrays.


Complexity Analysis

  • Time Complexity: Let be the number of stickers, and be the number of letters in the target word. A bound for time complexity is : for each sticker, we'll have to try using it up to times, and updating our target count costs , which we do up to times. Alternatively, since the answer is bounded at , we can prove that we can only search up to times. This would be .

  • Space Complexity: , to store stickersCount, targetCount, and handle the recursive callstack when calling search.



Approach 2: Dynamic Programming


Intuition

Suppose we need dp[state] stickers to satisfy all target[i]'s for which the i-th bit of state is set. We would like to know dp[(1 << len(target)) - 1].


Algorithm

For each state, let's work with it as now and look at what happens to it after applying a sticker. For each letter in the sticker that can satisfy an unset bit of state, we set the bit (now |= 1 << i). At the end, we know now is the result of applying that sticker to state, and we update our dp appropriately.

When using Python, we will need some extra techniques from Approach #1 to pass in time.


Complexity Analysis

  • Time Complexity: where be the total number of letters in all stickers, and be the number of letters in the target word. We can examine each loop carefully to arrive at this conclusion.

  • Space Complexity: , the space used by dp.



Analysis written by: @awice. Approach #2 inspired by @dreamoon.