边界情况

Generalized Abbreviation

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

Example:

Given word = "word", return the following list (order does not matter):

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

class Solution {
public:
    vector<string> generateAbbreviations(string word) {
        vector<string> ret;

        ret.push_back(word.length()>0?to_string(word.length()):"");

        for(int i=0;i<word.length();i++)
        {
            string left=(i==0)?"":to_string(i);
            vector<string> tmp=generateAbbreviations(word.substr(i+1));
            for(int j=0;j<tmp.size();j++)
            {

                ret.push_back(left+word[i]+tmp[j]);
            }
        }
        return ret;
    }
};

results matching ""

    No results matching ""