<p>We are given <code>n</code> different types of <code>stickers</code>. Each sticker has a lowercase English word on it.</p>

<p>You would like to spell out the given string <code>target</code> by cutting individual letters from your collection of stickers and rearranging them. You can use each sticker more than once if you want, and you have infinite quantities of each sticker.</p>

<p>Return <em>the minimum number of stickers that you need to spell out </em><code>target</code>. If the task is impossible, return <code>-1</code>.</p>

<p><strong>Note:</strong> In all test cases, all words were chosen randomly from the <code>1000</code> most common US English words, and <code>target</code> was chosen as a concatenation of two random words.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> stickers = [&quot;with&quot;,&quot;example&quot;,&quot;science&quot;], target = &quot;thehat&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong>
We can use 2 &quot;with&quot; stickers, and 1 &quot;example&quot; sticker.
After cutting and rearrange the letters of those stickers, we can form the target &quot;thehat&quot;.
Also, this is the minimum number of stickers necessary to form the target string.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> stickers = [&quot;notice&quot;,&quot;possible&quot;], target = &quot;basicbasic&quot;
<strong>Output:</strong> -1
Explanation:
We cannot form the target &quot;basicbasic&quot; from cutting letters from the given stickers.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == stickers.length</code></li>
	<li><code>1 &lt;= n &lt;= 50</code></li>
	<li><code>1 &lt;= stickers[i].length &lt;= 10</code></li>
	<li><code>1 &lt;= target.length &lt;= 15</code></li>
	<li><code>stickers[i]</code> and <code>target</code> consist of lowercase English letters.</li>
</ul>

# unbounded (repetition allowed)
# -> does that mean greedy? choose best sticker until target is reached.
#   no - because I can reach multiple different "two chars remaining", but one could require one sticker and another could require two sticker.
# It must be BFS. NOTE "DP" - I can't really think of a way to do it with DP?
# let's try BFS. generate all ways of using one sticker, all ways of using two stickers, etc. similar to word ladder.
# but what is "seen"? should it be all ways of gettings these characters.

# NOTE again on DP - I would have tried some sort of DFS with memo on total chars consumed.
#   but then note since it's DFS, it may not be the best way to reach those characters. BFS is the way.

# TLE
from collections import Counter, deque

class Solution:
    def minStickers(self, A, target) -> int:
        if set(target) - set(c for word in A for c in word):
            return -1
        seen = set(["".join(sorted(target))])
        q = deque([[Counter(target), "".join(sorted(target)), 0]])
        while True:
            cnt, chars, num = q.popleft()
            if not cnt:
                return num
            for w in A:
                chars = str("".join(sorted(chars + w)))
                if chars not in seen:
                    seen.add(chars)
                    q.append([cnt - Counter(w), chars, num+1])



# now let's try to optimize more... because this is exponential 2^n (try every combination)
#   eg - can remove all unnecessary letters and words.
class Solution:
    def minStickers(self, A, target) -> int:
        if set(target) - set(c for word in A for c in word):
            return -1
        # optimization 1 - remove letters and words that aren't necessary.
        # optimization 2 - remove words that are covered by others.
        A = [Counter(w) & Counter(target) for w in A]
        for i in range(len(A)):
            for j in range(i+1, len(A)):
                if A[i] & A[j] == A[i]:
                    A[i] = Counter()
                elif A[i] & A[j] == A[j]:
                    A[j] = Counter()
        A = ["".join(sorted(w.elements())) for w in A]
        A = sorted(set(A), key=lambda w: -len(w))  # might as well do longer words first
        # print(A)
        seen = set(["".join(sorted(target))])
        q = deque([[Counter(target), "".join(sorted(target)), 0]])
        while True:
            cnt, chars, num = q.popleft()
            if not cnt:
                return num
            for w in A:
                chars = str("".join(sorted(chars + w)))
                if chars not in seen:
                    seen.add(chars)
                    q.append([cnt - Counter(w), chars, num+1])


77470