clueless coding // TODO: be smarter

LeetCode Generalized Abbreviation: CPP Solution



TL;DR Code Solution


void helper(vector<string>& result, string word, string cur, int index, int abbr) {

    if (index == word.length()) {
        if (abbr > 0) {
            cur += to_string(abbr);
        }
        result.push_back(cur);
        return;
    }

    string newer = cur;
    newer += abbr ? to_string(abbr) : "";
    newer += word[index];
    helper(result, word, newer, index + 1, 0);
    helper(result, word, cur, index + 1, abbr + 1);

}

vector<string> generateAbbreviations(string word) {
    vector<string> result;
    string cur = "";
    helper(result, word, cur, 0, 0);
    return result;
}