<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 {
public:
    vector<string> stickers;
    vector<unordered_map<char, int>> sticker_counters;
    string target;
    vector<int> dp;


    void get_pruned_sticker(string &s, unordered_set<char>& t){
        string pruned;
        for(auto c : s){
            if(t.count(c)) pruned.append(1, c);
        }
        s = move(pruned);
    }

        bool is_sticker_subset(string s, string t){
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        if(s == t) return false;
        unordered_map<char, int> mp;
        for(auto c : s) mp[c]++;
        for(auto c : t){
            if(!mp.count(c)) continue;
            mp[c]--;
            if(mp.at(c) == 0) mp.erase(c);
        }
        return mp.size() == 0;
    }

    bool is_sticker_subset(int idx){
        bool ans = false;
        for(int i = 0;i < stickers.size(); i++){
            if(i == idx) continue;
            ans = ans || is_sticker_subset(stickers[idx], stickers[i]);
        }
        return ans;
    }

    unordered_map<char, int> get_counter(string& s){
        unordered_map<char, int> mp;
        for(auto c : s) mp[c]++;
        return mp;
    }

    void preprocess(){
        unordered_set<char> targetChars = unordered_set<char>(target.begin(), target.end());
        vector<unordered_map<char, int>> valid_stickers;
          for(auto &s : stickers){
              get_pruned_sticker(s, targetChars);
        }

        for(int i = 0;i < stickers.size(); i++){
            if(is_sticker_subset(i)) continue;
            valid_stickers.push_back(get_counter(stickers[i]));
        }
        sticker_counters = valid_stickers;
    }

    bool canUse(unordered_map<char, int> sticker, int mask){
        bool ans = false;
        for(int i  = 0;i < target.length(); i++){
            if(((1 << i) & mask) && sticker[target[i]]) ans = true;
        }
        return ans;
    }

    int updateMask(unordered_map<char, int> sticker, int mask){
        int new_mask = mask;
        for(int i = 0;i < target.length(); i++){
            if(((1 << i) & mask) && sticker.count(target[i])){
                new_mask = new_mask ^ (1 << i);
                sticker[target[i]]--;
                if(sticker.at(target[i]) == 0) sticker.erase(target[i]);
            }
        }
        return new_mask;
    }

    int f(int mask){
        if(mask == 0) return 0;
        if(dp[mask] != -1) return dp[mask];
        int ans = INT_MAX;
        for(auto sticker : sticker_counters){
            if(canUse(sticker, mask)){
                int new_mask = updateMask(sticker, mask);
                int temp = INT_MAX;
                int result = (temp = f(new_mask)) == INT_MAX ? INT_MAX : 1 + temp;
                ans = min (ans, result);
            }
        }
        return dp[mask] = ans;
    }


    int minStickers(vector<string>& stickers, string target) {
        this->stickers = stickers;
        this->target = target;
        
        preprocess();        
        int n = target.size();
        dp.assign(1 << n, -1);
        return f((1 << n) - 1) == INT_MAX ? -1 : dp[(1 << n) - 1];
    }
};


/*
stickers[]
t
f(i, reuse):
    if(t[i] is in reuse)
        resuse.remove(t[i])
        f(i+1, resuse)
    else
        for(t[i] in sticker)
            reuse.add(s - t[i])
            1 + f(i+1,resuse)


How to apply pruning here?

T -> t * 20^26
T2 -> 2^15
*/
223046