-
Notifications
You must be signed in to change notification settings - Fork 28
/
Stickers_to_Spell_Word.cpp
129 lines (123 loc) · 4.25 KB
/
Stickers_to_Spell_Word.cpp
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
// using target[indx] as loop driver
class Solution {
bool isValid(string const& sticker, char ch) {
for(int i = 0; i < sticker.length(); i++) {
if(sticker[i] == ch) {
return true;
}
}
return false;
}
int minStickers(int indx, int mask, const int MAX, string const& target, vector<string> const& stickers, vector<vector<int>>& dp) {
if(indx == target.length()) {
if(mask == MAX) {
return 0;
}
return INT_MAX;
}
if(dp[indx][mask] != -1) {
return dp[indx][mask];
}
int ret = INT_MAX;
if(mask & (1 << indx)) {
return dp[indx][mask] = minStickers(indx + 1, mask, MAX, target, stickers, dp);
}
for(int j = 0; j < stickers.size(); j++) {
if(isValid(stickers[j], target[indx])) {
unordered_map<int, int> freq;
for(int k = 0; k < stickers[j].length(); k++) {
freq[stickers[j][k]]++;
}
int newMask = mask;
for(int l = 0; l < target.length(); l++) {
if(!(mask & (1 << l))) {
if(freq[target[l]] > 0) {
freq[target[l]]--;
newMask ^= (1 << l);
}
}
}
int tmp = min(ret, minStickers(indx + 1, newMask, MAX, target, stickers, dp));
if(tmp != INT_MAX) {
ret = min(ret, 1 + tmp);
}
}
}
return dp[indx][mask] = ret;
}
public:
int minStickers(vector<string>& stickers, string target) {
int n = (int) target.length();
int maxMask = (1 << n) - 1;
vector<vector<int>> dp(n, vector<int>(maxMask + 1, -1));
int result = minStickers(0, 0, maxMask, target, stickers, dp);
if(result == INT_MAX) {
return -1;
}
return result;
}
};
// using sticker[indx] as loop driver
class Solution {
bool isValid(string const& sticker, string const& target, int mask) {
unordered_map<char, int> Map;
for(int i = 0; i < target.length(); i++) {
if(!(mask & (1 << i))) {
Map[target[i]]++;
}
}
for(int i = 0; i < sticker.length(); i++) {
if(Map[sticker[i]]) {
return true;
}
}
return false;
}
int minStickers(int indx, int mask, const int MAX, string const& target, vector<string> const& stickers, vector<vector<int>>& dp) {
if(mask == MAX) {
return 0;
}
if(indx == stickers.size()) {
return mask == MAX ? 0 : INT_MAX;
}
if(dp[indx][mask] != -1) {
return dp[indx][mask];
}
int ret = INT_MAX;
if(isValid(stickers[indx], target, mask)) {
unordered_map<int, int> freq;
for(int k = 0; k < stickers[indx].length(); k++) {
freq[stickers[indx][k]]++;
}
int newMask = mask;
for(int l = 0; l < target.length(); l++) {
if(!(mask & (1 << l))) {
if(freq[target[l]] > 0) {
freq[target[l]]--;
newMask |= (1 << l);
}
}
}
int tmp = min(ret, min(minStickers(indx, newMask, MAX, target, stickers, dp), minStickers(indx + 1, newMask, MAX, target, stickers, dp)));
if(tmp != INT_MAX) {
ret = min(ret, 1 + tmp);
}
}
int tmp = min(ret, minStickers(indx + 1, mask, MAX, target, stickers, dp));
if(tmp != INT_MAX) {
ret = min(ret, tmp);
}
return dp[indx][mask] = ret;
}
public:
int minStickers(vector<string>& stickers, string target) {
int n = (int) target.length();
int maxMask = (1 << n) - 1;
vector<vector<int>> dp(stickers.size(), vector<int>(maxMask + 1, -1));
int result = minStickers(0, 0, maxMask, target, stickers, dp);
if(result == INT_MAX) {
return -1;
}
return result;
}
};