<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 {
private:
    int min_stickers = -1;
    unordered_map<string, int> memo;
public:
    int backtrack(string curr, unordered_map<int, unordered_map<char, int>>& freq, int num_stickers) {
        // check if already memoized
        if (memo.find(curr) != memo.end()) {
            min_stickers = min(min_stickers, memo[curr] + num_stickers);
            return memo[curr];
        }
        if (curr == "") {
            if (min_stickers == -1) {
                min_stickers = num_stickers;
            } else {
                min_stickers = min(min_stickers, num_stickers);
            }
            memo[curr] = 0;
            return 0;
        }
        num_stickers++;
        int memo_val = -1;
        for(int i=0;i<freq.size();i++) {
            // Optimization: skip stickers that don't contain the first letter of tar
            if (freq[i].count(curr[0]) == 0) continue;
            unordered_map<char, int> curr_freq;
            string new_str = "";
            for(int j=0;j<curr.size();j++) {
                if(curr_freq[curr[j]] < freq[i][curr[j]]) {
                    curr_freq[curr[j]]++;
                } else {
                    // add it to the new string
                    new_str += curr[j];
                }
            }
            if (new_str.size() != curr.size()) {
                int res = 1 + backtrack(new_str, freq, num_stickers);
                if (memo_val == -1) {
                    memo_val = res;
                } else {
                    memo_val = min(memo_val, res);
                }
            }
        }
        memo[curr] = memo_val;
        return memo_val;
    }
    int minStickers(vector<string>& stickers, string& target) {
        //first, check if valid
        unordered_map<int, unordered_map<char, int>> freq;
        for(int i=0;i<stickers.size();i++) {
            for(int j=0;j<stickers[i].size();j++) {
                freq[i][stickers[i][j]]++;
            }
        }
        for(int i=0;i<target.size();i++) {
            bool found = false;
            for(int j=0;j<stickers.size();j++) {
                if (freq[j].find(target[i]) != freq[j].end()) {
                    found = true;
                }
            }
            if (!found) return -1;
        }
        backtrack(target, freq, 0);
        return min_stickers;
    }
};
212041