Why I got an output limited exceeded?


  • 0

    Straight forward solution:

    class Solution {
        int sz;
    public:
        vector<string> generateAbbreviations(string word) {
            sz = (int) word.size();
            vector<string> ret;
            string path = "";
            backtracking(word,path,ret,0);
            return ret;
        }
        
        void backtracking(string &s,string &path, vector<string> &ret, int i){
            if(i>=sz) ret.push_back(path);
            else{
                for(int j = i; j <= sz; j++){
                    if(j>i) path += to_string(j-i);
                    if(j<sz) path += s[j];
                    backtracking(s,path,ret,j+1);
                    while(path.size()>i) path.pop_back();
                }
            }
        }
    };
    

Log in to reply
 

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