Easy to understand C++ solution


  • 0
    Z
    class Solution {
    public:
    vector<string> generateAbbreviations(string word) {
        vector<string> res;
        //res.push_back(word);
        BT(res,word,"",0);
        return res;
    }
    
    void BT(vector<string>& res,string word,string save,int prenum){
        //prenum tells whether the prev position is a number or not,1 means true, 0 means false
        if(word.size()==0) {
            res.push_back(save);
            return;
        }
        for(int i=0;i<=(word.size()*(!prenum));i++){
            if(i==0) {
    
                BT(res,word.substr(1,word.size()-1),save+word[0],0);
            }
            else{
            BT(res,word.substr(i,word.size()-i),save+to_string(i),1);
            }
            
        }
        
    }
    };

Log in to reply
 

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