[LeetCode] Word Abbreviation

527. Word Abbreviation

Given an array of distinct strings words, return the minimal possible abbreviations for every word.

The following are the rules for a string abbreviation:

  1. Begin with the first character, and then the number of characters abbreviated, followed by the last character.
  2. If there is any conflict and more than one word shares the same abbreviation, a longer prefix is used instead of only the first character until making the map from word to abbreviation become unique. In other words, a final abbreviation cannot map to more than one original word.
  3. If the abbreviation does not make the word shorter, then keep it as the original.
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
struct Trie {
Trie* next[26];
int cnt;
public:
Trie():cnt(0){
memset(next,0,sizeof(next));
}
void insert(string str, int i) {
if(i != str.length()) {
if(!next[str[i]-'a']) next[str[i]-'a'] = new Trie();
next[str[i]-'a']->insert(str,i+1);
}
cnt++;
}
int find(string str, int i) {
if(i == str.length()) return i;
if(cnt == 1) return i-1;
return next[str[i]-'a']->find(str,i+1);
}

};
class Solution {
string abbreviation(string str) {
string abb = str[0] + to_string(str.length()-2) + str[str.length() - 1];
if(abb.length() < str.length()) return abb;
return str;
}
public:
vector<string> wordsAbbreviation(vector<string>& words) {
unordered_map<string, vector<pair<int,string>>> table;
vector<string> res(words.size());
for(int i = 0; i < words.size(); i++) {
string abb = abbreviation(words[i]);
table[abb].push_back({i,words[i]});
res[i] = abb;
}


for(auto [k, v]: table) {
if(v.size() == 1) continue;
Trie* trie = new Trie();
for(auto [_, str] : v) {
trie->insert(str,0);
}
for(auto [idx, str] : v) {
auto sep = trie->find(str,0);
res[idx] = str.substr(0,sep) + abbreviation(str.substr(sep));
}
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/15/PS/LeetCode/word-abbreviation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.