Leetcode 320 Solution

This article provides solution to leetcode question 320 (generalized-abbreviation)

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

Solution

class Solution {
public:
    vector<string> generateAbbreviations(string word) {
        vector<string> res;
        res.push_back(word);

        if (word.empty() == false)
        {
            for (int i = 0; i < word.size(); i++)
            {
                for (int j = i; j < word.size(); j++)
                {
                    if (j == word.size() - 1)
                        res.push_back(word.substr(0, i) + to_string(j - i + 1));
                    else
                    {
                        auto ch = word[j + 1];
                        auto subres = generateAbbreviations(word.substr(j + 2));
                        for (auto sub : subres)
                            res.push_back(word.substr(0, i) + to_string(j - i + 1) + ch + sub);
                    }
                }
            }
        }

        return res;
    }
};