# 320. Generalized Abbreviation

Write a function to generate the generalized abbreviations of a word.

Note: The order of the output does not matter.

Example:

``````Input: "word"
Output:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
``````

0000 word
0001 wor1
0010 wo1d
0011 wo2
0100 w1rd
0101 w1r1
0110 w2d
0111 w3
1000 1ord
1001 1or1
1010 1o1d
1011 1o2
1100 2rd
1101 2r1
1110 3d
1111 4

``````class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = 0; i < pow(2, word.size()); ++i) {
string out = "";
int cnt = 0, t = i;
for (int j = 0; j < word.size(); ++j) {
if (t & 1 == 1) {
++cnt;
if (j == word.size() - 1) out += to_string(cnt);
} else {
if (cnt != 0) {
out += to_string(cnt);
cnt = 0;
}
out += word[j];
}
t >>= 1;
}
res.push_back(out);
}
return res;
}
};
``````

[“word”,”1ord”,”w1rd”,”2rd”,”wo1d”,”1o1d”,”w2d”,”3d”,”wor1”,”1or1”,”w1r1”,”2r1”,”wo2”,”1o2”,”w3”,”4”]

``````class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = 0; i < pow(2, word.size()); ++i) {
string out = "";
int cnt = 0;
for (int j = 0; j < word.size(); ++j) {
if ((i >> j) & 1) ++cnt;
else {
if (cnt != 0) {
out += to_string(cnt);
cnt = 0;
}
out += word[j];
}
}
if (cnt > 0) out += to_string(cnt);
res.push_back(out);
}
return res;
}
};
``````

``````class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res{word};
helper(word, 0, res);
return res;
}
void helper(string word, int pos, vector<string> &res) {
for (int i = pos; i < word.size(); ++i) {
for (int j = 1; i + j <= word.size(); ++j) {
string t = word.substr(0, i);
t += to_string(j) + word.substr(i + j);
res.push_back(t);
helper(t, i + 1 + to_string(j).size(), res);
}
}
}
};
``````

[“word”,”1ord”,”1o1d”,”1o2”,”1or1”,”2rd”,”2r1”,”3d”,”4”,”w1rd”,”w1r1”,”w2d”,”w3”,”wo1d”,”wo2”,”wor1”]

``````class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
helper(word, 0, 0, "", res);
return res;
}
void helper(string word, int pos, int cnt, string out, vector<string> &res) {
if (pos == word.size()) {
if (cnt > 0) out += to_string(cnt);
res.push_back(out);
} else {
helper(word, pos + 1, cnt + 1, out, res);
helper(word, pos + 1, 0, out + (cnt > 0 ? to_string(cnt) : "") + word[pos], res);
}
}
};
``````

[“4”,”3d”,”2r1”,”2rd”,”1o2”,”1o1d”,”1or1”,”1ord”,”w3”,”w2d”,”w1r1”,”w1rd”,”wo2”,”wo1d”,”wor1”,”word”]

``````class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
res.push_back(word.size() == 0 ? "" : to_string(word.size()));
for (int i = 0; i < word.size(); ++i) {
for (auto a : generateAbbreviations(word.substr(i + 1))) {
string left = i > 0 ? to_string(i) : "";
res.push_back(left + word.substr(i, 1) + a);
}
}
return res;
}
};
``````

[“4”,”w3”,”wo2”,”wor1”,”word”,”wo1d”,”w1r1”,”w1rd”,”w2d”,”1o2”,”1or1”,”1ord”,”1o1d”,”2r1”,”2rd”,”3d”]

https://leetcode.com/problems/generalized-abbreviation/

https://leetcode.com/discuss/75754/java-backtracking-solution

https://leetcode.com/discuss/77280/c-straightforward-recursive-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation