<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 my_compare {
public:
    bool operator()(const pair<char, int> a1, const pair<char, int> a2) const {
        return a1.second < a2.second;
    }
};

class Solution {
    unordered_map<char, int> t;
    unordered_map<char, vector<int>> idx;
    vector<unordered_map<char, int>> s;
    int res;
    vector<pair<char, int>> ordered_idx;

    void helper(int cur, int pos, int cand, unordered_map<char, int> left_v) {
        if (pos == ordered_idx.size()) {
            res = res < cur ? res : cur;
            return;
        }
        if (left_v[ordered_idx[pos].first] == 0)
            helper(cur, pos + 1, 0, left_v);

        char key = ordered_idx[pos].first;
        int id = idx[key][cand];
        if (cand == idx[key].size() - 1) {
            int n = (left_v[key] + s[id][key] - 1) / s[id][key];
            if (cur + n > res)
                return;
            for (auto k : s[id]) {
                left_v[k.first] -= (n * k.second);
                if (left_v[k.first] < 0)
                    left_v[k.first] = 0;
            }
            helper(cur + n, pos + 1, 0, left_v);
        } else {
            int m = (left_v[key] + s[id][key] - 1) / s[id][key];
            for (int n = 0; n <= m; n++) {
                if (n > 0) {
                    for (auto k : s[id]) {
                        left_v[k.first] -= (k.second);
                        if (left_v[k.first] < 0)
                            left_v[k.first] = 0;
                    }
                }

                if (cur + n > res)
                    return;
                if (n != m) {
                    helper(cur + n, pos, cand + 1, left_v);
                } else {
                    helper(cur + n, pos + 1, 0, left_v);
                }
            }
        }
        return;
    }

    int domi(unordered_map<char, int>& n1, unordered_map<char, int>& n2) {
        int res = 0;

        if (n1.size() == n2.size()) {
            for (auto item_n1 : n1) {
                if (n2.find(item_n1.first) == n2.end())
                    return 4;
                if (n2[item_n1.first] == item_n1.second) {
                    if (res == 0) {
                        res = 3;
                    }
                } else if (n2[item_n1.first] > item_n1.second) {
                    if (res == 0 || res == 3) {
                        res = 2;
                    } else if (res == 1) {
                        return 4;
                    }
                } else {
                    if (res == 0 || res == 3) {
                        res = 1;
                    } else if (res == 2) {
                        return 4;
                    }
                }
            }
            return res;
        }

        for (auto item_n1 : n1) {
            if (n2.find(item_n1.first) == n2.end())
                return 4;
            if (n2[item_n1.first] == item_n1.second) {
                if (res == 0) {
                    res = 3;
                }
            } else if (n2[item_n1.first] > item_n1.second) {
                res = 2;
            } else {
                return 4;
            }
        }
        return res;
    }

public:
    int minStickers(vector<string>& stickers, string target) {
        t.clear();
        idx.clear();
        res = target.size();
        for (auto c : target) {
            if (t.find(c) == t.end()) {
                t[c] = 1;
            } else {
                t[c]++;
            }
        }

        for (int i = 0; i < stickers.size(); i++) {
            unordered_map<char, int> tmp;
            for (auto c : stickers[i]) {
                if (t.find(c) != t.end()) {
                    if (tmp.find(c) == tmp.end()) {
                        tmp[c] = 1;
                    } else if (tmp[c] < t[c]) {
                        tmp[c]++;
                    }
                }
            }

            int j = 0;
            bool add = 1;
            while (j < s.size()) {
                int dom = 0;
                if (s[j].size() == tmp.size()) {
                    dom = domi(s[j], tmp);
                    if (dom == 1 || dom == 3) {
                        add = 0;
                        break;
                    } else if (dom == 2) {
                        s.erase(s.begin() + j);
                    } else {
                        j++;
                    }
                } else if (s[j].size() < tmp.size()) {
                    dom = domi(s[j], tmp);
                    if (dom == 2 || dom == 3) {
                        s.erase(s.begin() + j);
                    } else {
                        j++;
                    }
                } else {
                    dom = domi(tmp, s[j]);
                    if (dom == 3 || dom == 2) {
                        add = 0;
                        break;
                    } else {
                        j++;
                    }
                }
            }
            if (add)
                s.push_back(tmp);
        }

        for (int i = 0; i < s.size(); i++) {
            for (auto c_item : s[i]) {
                if (idx.find(c_item.first) == idx.end()) {
                    idx[c_item.first] = {i};
                } else {
                    idx[c_item.first].push_back(i);
                }
            }
        }

        if (idx.size() < t.size())
            return -1;

        ordered_idx.clear();
        for (auto it = idx.begin(); it != idx.end(); it++) {
            ordered_idx.push_back({it->first, (it->second).size()});
        }

        sort(ordered_idx.begin(), ordered_idx.end(), my_compare());

        helper(0, 0, 0, t);

        return res;
    }
};
415633