<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>

class Solution:
    # learnt from others' code
    # set operations:
    # Intersection: & eg. {e, b} & {e, b, c} = {e, b}
    # Union: | eg. {e, b} & {e, b, c} = {e, b, c}
    # difference: - eg. {e, b} & {e, b, c} = {}
    # difference: - eg. {e, b, c} & {e, b} = {c}
    # check if set_a is part of set b, we can do set_a == set_a & set_b

    # fast bfs
    def minStickers_bfs(self, stickers, target):
        buckets = [Counter(sticker) for sticker in stickers]
        n = len(buckets)

        q = deque([target])
        seen = set([target])
        res = 0

        while q:
            l = len(q)
            res +=1
            for _ in range(l):
                cur = q.popleft()
                
                for bucket in buckets:
                    # since we need to remove all the characters from the target
                    # we can exam the bucket, and see if the first character is in the bucket
                    # it would greatly reduce the time, 
                    # since it helps to get rid of buckets that doesn't contain target characters
                    if bucket[cur[0]] == 0:
                        continue

                    next_str = cur

                    for c in bucket:
                        if c in next_str:
                            next_str = next_str.replace(c, '', bucket[c])
                    
                    # we want the target to become an empty string
                    if next_str == '':
                        return res

                    if next_str not in seen:
                        seen.add(next_str)
                        q.append(next_str)
        return -1

    def minStickers(self, stickers: List[str], target: str) -> int:
        @cache
        def memo(index, cur_target):
            if cur_target == '':
                return 0
    
            if index == n:
                return inf

            res = memo(index + 1, cur_target)

            new_target = cur_target
            bucket = buckets[index]

            for c in bucket:
                new_target = new_target.replace(c, '', bucket[c])

            # if we removed something, we should try the cur sticker again
            if new_target != cur_target:
                res = min(res, 1 + memo(index, new_target))

            return res
        
        n = len(stickers)
        buckets = [Counter(sticker) for sticker in stickers]
        res = memo(0, target)
        return -1 if res == inf else res


    def minStickers_search(self, stickers: List[str], target: str) -> int:
        def backtrack(cur):
            if cur >= self.res:
                return

            # if i >= len(buckets):
            #     return cur if all([v <= 0 for v in counter.values()]) else inf
            if not buckets:
                if all([v <= 0 for v in counter.values()]):
                    self.res = cur
                return
    
            # for i in range(index, len(buckets)):

            bucket = buckets.pop()
            used = max((counter[letter] - 1) // bucket[letter] + 1
                        for letter in bucket)
            used = max(used, 0)

            for c in bucket:
                counter[c] -= used * bucket[c]

            backtrack(cur + used)
            for i in range(used - 1, -1, -1):
                for letter in bucket:
                    counter[letter] += bucket[letter]
                backtrack(cur + i)

            buckets.append(bucket)

        buckets = []
        n = len(stickers)
        counter = Counter(target)

        for sticker in stickers:
            sticker_counter = Counter()
            for c in sticker:
                if c in counter:
                    sticker_counter[c] += 1

            buckets.append(sticker_counter)

        # print(buckets)
        # print(counter)

        # range from backwards to avoid index out of range error
        # since we are poping out dictionary out of the list
        for i in range(n-1, -1, -1):
            if any(buckets[i] == buckets[i] & buckets[j] for j in range(len(buckets)) if i != j):
                buckets.pop(i)

        self.res = inf
        backtrack(0)
        return -1 if self.res == inf else self.res
        


70068