320. Generalized Abbreviation

Posted jxr041100

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了320. 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>res;
        dfs(0,"",res,false,word);
        return res;
    }
private:
    void dfs(int idx,string tmp,vector<string>&res,bool prevNum,string word)
    {
        if(idx==word.size()){
            res.push_back(tmp);
            return;
        }
        dfs(idx+1,tmp+word[idx],res,false,word);
        if(!prevNum)
        {
            for(int len = 1;len+idx<=word.size();len++)
            {
                dfs(idx+len,tmp+to_string(len),res,true,word);
            }
        }
    }
};

 

以上是关于320. Generalized Abbreviation的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 320: Generalized Abbreviation

320. Generalized Abbreviation

320. Generalized Abbreviation-- back tracking

LeetCode Generalized Abbreviation

[Locked] Generalized Abbreviation

Generalized Abbreviation