forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-unique-word-abbreviation.cpp
132 lines (123 loc) · 3.96 KB
/
minimum-unique-word-abbreviation.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
130
131
132
// Time: O((d + n) * 2^n)
// Space: O(d)
// optimized from Solution2
class Solution {
public:
string minAbbreviation(string target, vector<string>& dictionary) {
vector<int> diffs;
dictionary_to_diffs(target, dictionary, &diffs);
if (empty(diffs)) {
return to_string(size(target));
}
int total = 1 << size(target);
int result = total - 1;
for (int mask = 0; mask < total; ++mask) {
if (all_of(cbegin(diffs), cend(diffs), [&mask](int d) { return d & mask; })) {
if (bits_to_abbr_len(target, mask) < bits_to_abbr_len(target, result)) {
result = mask;
}
}
}
return bits_to_abbr(target, result);
}
private:
void dictionary_to_diffs(const string& target, const vector<string>& dictionary,
vector<int> *diffs) {
for (const auto& word : dictionary) {
if (size(word) != size(target)) {
continue;
}
int bits = 0;
for (int i = 0; i < size(word); ++i) {
if (target[i] != word[i]) {
bits |= 1 << i;
}
}
diffs->emplace_back(bits);
}
}
int bits_to_abbr_len(const string& target, int bits) {
int total = 0;
for (int i = 0, pre = 0; i < size(target); ++i, bits >>= 1) {
if (bits & 1) {
if (i - pre > 0) {
total += size(to_string(i - pre));
}
pre = i + 1;
++total;
} else if (i == size(target) - 1) {
total += size(to_string(i - pre + 1));
}
}
return total;
}
string bits_to_abbr(const string& target, int bits) {
string abbr;
for (int i = 0, pre = 0; i < size(target); ++i, bits >>= 1) {
if (bits & 1) {
if (i - pre > 0) {
abbr += to_string(i - pre);
}
pre = i + 1;
abbr.push_back(target[i]);
} else if (i == size(target) - 1) {
abbr += to_string(i - pre + 1);
}
}
return abbr;
}
};
// Time: O((d + n) * 2^n)
// Space: O(d + n)
class Solution2 {
public:
string minAbbreviation(string target, vector<string>& dictionary) {
vector<int> diffs;
dictionary_to_diffs(target, dictionary, &diffs);
if (empty(diffs)) {
return to_string(size(target));
}
int total = 1 << size(target);
string result = target;
for (int mask = 0; mask < total; ++mask) {
if (all_of(cbegin(diffs), cend(diffs), [&mask](int d) { return d & mask; })) {
auto abbr = bits_to_abbr(target, mask);
if (size(abbr) < size(result)) {
result = move(abbr);
}
}
}
return result;
}
private:
void dictionary_to_diffs(const string& target, const vector<string>& dictionary,
vector<int> *diffs) {
for (const auto& word : dictionary) {
if (size(word) != size(target)) {
continue;
}
int bits = 0;
for (int i = 0; i < size(word); ++i) {
if (target[i] != word[i]) {
bits |= 1 << i;
}
}
diffs->emplace_back(bits);
}
}
string bits_to_abbr(const string& target, int bits) {
string abbr;
for (int i = 0, pre = 0; i < size(target); ++i, bits >>= 1) {
if (bits & 1) {
if (i - pre > 0) {
abbr += to_string(i - pre);
}
pre = i + 1;
abbr.push_back(target[i]);
} else if (i == size(target) - 1) {
abbr += to_string(i - pre + 1);
}
}
return abbr;
}
};