320. Generalized Abbreviation
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = 0; i < (1 << word.size()); i++)
res.push_back(buildAbbr(word, i));
return res;
}
private:
string buildAbbr(string &word, int k) {
string res = "";
int count = 0;
for (int i = word.size() - 1; i >= 0 ; i--) {
if ((k & 1) == 0) count++; //aaaaaa if (k & 1 == 0) is wrong!!!!
else {
if (count) res = to_string(count) + res;
res = word[i] + res;
count = 0;
}
k >>= 1;
}
if (count) res = to_string(count) + res;
return res;
}
};