-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminAbbreviation.cpp
More file actions
60 lines (59 loc) · 1.8 KB
/
minAbbreviation.cpp
File metadata and controls
60 lines (59 loc) · 1.8 KB
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
class Solution {
public:
/**
* @param target: a target string
* @param dictionary: a set of strings in a dictionary
* @return: an abbreviation of the target string with the smallest possible length
*/
string minAbbreviation(string &target, vector<string> &dictionary) {
int n = target.size();
unordered_set<string> dict;
for (auto s: dictionary) {
if (s.size() == n) dict.insert(s);
}
vector<pair<int, int> > abbr;
for(int i = 0; i < (1<<n); i++) abbr.push_back({getLen(i, n), i});
sort(abbr.begin(), abbr.end());
for (auto p: abbr) {
int mask = p.second, flag = 1;
string t = getAbbr(mask, target);
for (string s: dict) {
if (t == getAbbr(mask, s)) {
flag = 0;
break;
}
}
if (flag) return t;
}
return "";
}
string getAbbr(int mask, string &s) {
string ret;
int n = s.size(), i = n - 1, count0 = 0;
for (; i >= 0 && mask > 0; i--, mask = mask >> 1) {
if((mask & 1) == 1) {
if (count0 > 0) {
ret = to_string(count0) + ret;
count0 = 0;
}
ret = s[i] + ret;
}
else count0++;
}
if (i >= 0) ret = to_string(1 + i) + ret;
return ret;
}
int getLen(int mask, int n) {
int result = 0, flag = 0, i = n - 1;
for (; i >= 0 && mask > 0; i--, mask = mask >> 1) {
if((mask & 1) == 1) {
result++;
if (flag) result++;
flag = 0;
}
else flag = 1;
}
if (i >= 0) result++;
return result;
}
};