Leetcode 691 is an interesting problem, I didn’t notice that T <= 15. BFS is good enough (and even faster) for this, but dynamic programming with bit compression is the ultimate solution with more words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
class Solution { public: // 691. Stickers to spell word // Time: O(2^T * S * T), // T <= 15 // T is the number of letters in the target word, // S is the total number of letters in all stickers // Space: O(2^T) int minStickers(vector<string>& stickers, string target) { using Freq = vector<int>; const int MAX_ALPHABET = 26; const int T = target.size(); vector<Freq> freqs(stickers.size()); // calc frequency statistics auto buildFreqFromString = [=](const string &s) { Freq ans(MAX_ALPHABET); for (const auto &c : s) ++ans[c - 'a']; return ans; }; Freq targetFreq = buildFreqFromString(target); for (int i = 0; i < stickers.size(); ++i) { freqs[i] = buildFreqFromString(stickers[i]); // remove higher frequency than the target for (int j = 0; j < MAX_ALPHABET; ++j) freqs[i][j] = min(freqs[i][j], targetFreq[j]); } // test if a is the proper subset of b auto isProperSubset = [=](const Freq &a, const Freq &b) { for (int i = 0; i < a.size(); ++i) if (a[i] >= b[i]) return false; return true; }; // remove the proper subset of stickets for (int i = freqs.size() - 1; i >= 0; --i) { for (int j = 0; j < freqs.size(); ++j) if (i != j && isProperSubset(freqs[i], freqs[j])) freqs.erase(freqs.begin() + i); } auto getStringFromFreq = [=](const Freq &a) { string ans = ""; for (int i = 0; i < a.size(); ++i) { ans += string(a[i], 'a' + i); } return ans; }; vector<string> pieces(freqs.size()); for (int i = 0; i < freqs.size(); ++i) { pieces[i] = getStringFromFreq(freqs[i]); cout << i << " " << pieces[i] << endl; } const int MAX_T = 1 << T; vector<int> f(MAX_T, -1); f[0] = 0; for (int i = 0; i < MAX_T; ++i) { if (f[i] < 0) continue; for (const auto &piece : pieces) { int j = i; for (const auto &c : piece) { // find the first k that does not appear in state i for (int k = 0; k < T; ++k) { if ((j >> k) & 1) continue; if (target[k] == c) { j |= (1 << k); break; } } if (f[j] < 0 || f[j] > f[i] + 1) { f[j] = f[i] + 1; } } } } return f[MAX_T - 1]; } // BFS, slower version // Time: O(N^{T+1} T^2), N is the number of tickers, T is the number of letters in the target word // In fact, it's O(C(N + T - 1, T - 1) T^2) int minStickers2(vector<string>& stickers, string target) { using Freq = vector<int>; const int MAX_ALPHABET = 26; vector<Freq> freqs(stickers.size()); // calc frequency statistics auto buildFreqFromString = [=](const string &s) { Freq ans(MAX_ALPHABET); for (const auto &c : s) { ++ans[c - 'a']; } return ans; }; Freq targetFreq = buildFreqFromString(target); for (int i = 0; i < stickers.size(); ++i) { freqs[i] = buildFreqFromString(stickers[i]); // remove higher frequency than the target for (int j = 0; j < MAX_ALPHABET; ++j) { freqs[i][j] = min(freqs[i][j], targetFreq[j]); } } // test if a is the proper subset of b auto isProperSubset = [=](const Freq &a, const Freq &b) { for (int i = 0; i < a.size(); ++i) { if (a[i] >= b[i]) return false; } return true; }; // remove the proper subset of stickets for (int i = freqs.size() - 1; i >= 0; --i) { for (int j = 0; j < freqs.size(); ++j) { if (i != j && isProperSubset(freqs[i], freqs[j])) { freqs.erase(freqs.begin() + i); } } } using Item = pair<int, Freq>; // q stores the current step and current goal // the default queue queue<Item*> q; unordered_map<string, bool> vd; q.push(new Item(0, targetFreq)); vd[target] = true; auto hasOverlap = [=](const Freq &a, const Freq &b) { for (int i = 0; i < a.size(); ++i) { if (a[i] > 0 && b[i] > 0) return true; } return false; }; auto subtractFreq = [=](const Freq &a, const Freq &b) { Freq ans(26); for (int i = 0; i < a.size(); ++i) { ans[i] = max(0, a[i] - b[i]); } return ans; }; auto getStringFromFreq = [=](const Freq &a) { string ans = ""; for (int i = 0; i < a.size(); ++i) { ans += string(char(int('a') + i), a[i]); } return ans; }; auto isEmpty = [=](const Freq &a) { for (int i = 0; i < a.size(); ++i) if (a[i] > 0) return false; return true; }; // BFS while (!q.empty()) { auto current = q.front(); q.pop(); for (auto &freq : freqs) { if (!hasOverlap(current->second, freq)) continue; auto next = subtractFreq(current->second, freq); auto next_str = getStringFromFreq(next); if (isEmpty(next)) return current->first + 1; if (vd.find(next_str) == vd.end()) { q.push( new Item(current->first + 1, next) ); vd[next_str] = true; } } // we should delete the pointers, but not this time // other solutions are to use vectors instead of queue // and remove the vector in the end // delete current; } return -1; } }; |