forked from rui-yan/LeetCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-abbreviation.cpp
52 lines (46 loc) · 1.74 KB
/
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
// Time: O(n * l) ~ O(n^2 * l^2)
// Space: O(n * l)
class Solution {
public:
vector<string> wordsAbbreviation(vector<string>& dict) {
unordered_map<string, unordered_set<string>> abbr_to_word;
unordered_map<string, string> word_to_abbr;
for (const auto& word : dict) {
const auto prefix = word.substr(0, 1);
abbr_to_word[toAbbr(prefix, word)].emplace(word);
}
for (const auto& kvp : abbr_to_word) {
if (kvp.second.size() > 1) {
for (const auto& word : kvp.second) {
for (int i = 2; i < word.length(); ++i) {
const auto prefix = word.substr(0, i);
if (isUnique(prefix, kvp.second)) {
word_to_abbr[word] = toAbbr(prefix, word);
break;
}
}
}
} else {
word_to_abbr[*kvp.second.begin()] = kvp.first;
}
}
vector<string> result;
for (const auto& word : dict) {
result.emplace_back(word_to_abbr[word]);
}
return result;
}
private:
bool isUnique(const string& prefix, const unordered_set<string>& words) {
return 1 == count_if(words.begin(), words.end(),
[&prefix](const string& word) {
return !word.compare(0, prefix.length(), prefix);
});
}
string toAbbr(const string& prefix, const string& word) {
string abbr = prefix;
abbr += to_string(word.length() - 1 - prefix.length());
abbr += word.back();
return abbr.length() < word.length() ? abbr : word;
}
};