320. Generalized Abbreviation - cocoder39/coco39_LC GitHub Wiki
tip: use cnt
to record the number of letters that are transformed to abbr recently.
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
helper(res, word, 0, "", 0);
return res;
}
private:
void helper(vector<string>& res, string& word, int start, string cur, int cnt) {
if (start == word.length()) {
cur += cnt == 0 ? "" : to_string(cnt);
res.push_back(cur);
return;
}
helper(res, word, start + 1, cur, cnt + 1); //abbr
cur += cnt == 0 ? "" : to_string(cnt);
helper(res, word, start + 1, cur + word[start], 0);
}
};