C++ DFS concise solution


  • 0
    W
    class Solution {
    public:
        vector<string> generateAbbreviations(string word) {
        vector<string> res;
        generate(res,"",word,false);
        return res;
        }
    private:
        void generate(vector<string> &res,string curres,string word,bool isnum){
        if(word.empty()){
            res.push_back(curres);
            return ;
        }
        generate(res,curres+word.substr(0,1),word.substr(1),false);
        if(curres.empty()||!isnum)
            for(int i=1;i<=word.size();i++)
                generate(res,curres+to_string(i),word.substr(i),true);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.