97% c++ solution


  • 0

    class Solution {
    public:
    vector<string> generateAbbreviations(string word) {
    int n=word.size();

        vector<string> ans;
        
        if(n==0){
            ans.push_back("");
            return ans;
        }
        
       
        ans.push_back(word.substr(0,1));
        
        ans.push_back("1");
        
        if(n==1){
            return ans;
        }
        for(int i=1;i<n;i++){
             
            int size=ans.size();
            for(int j=0;j<size;j++){
                int len=ans[j].size();
                
                if(isdigit(ans[j][len-1])){
                    string sss;
                    if(len>=2&&isdigit(ans[j][len-2])){
                        int op=stoi(ans[j].substr(len-2,2));
                        op++;
                        sss=to_string(op);
                        sss=ans[j].substr(0,len-2)+sss;
                    }
                    else{
                        
                    
                        if(ans[j][len-1]=='9'){
                            sss="10";
                        }
                        else{
                            int op=stoi(ans[j].substr(len-1,1));
                            op++;
                            sss=to_string(op);
                        
                        }
                        sss=ans[j].substr(0,len-1)+sss;
                    }
                    
                    ans.push_back(sss);
                }
                else{
                    string temp=ans[j]+"1";
                    ans.push_back(temp);
                }
                ans[j]+=word[i];
                
                
                
            }
            
        }
        return ans;
        
    }
    

    };


  • 0

    I think your solution have some bugs cause you assume the biggest number less than 100, so the follow solution may free the bugs.

    class Solution {  
    public:
    vector<string> ans;
    
    vector<string> generateAbbreviations(string word) {
        int n = word.size();
        if(n == 0) {
            ans.push_back("");
            return ans;
        }
        
        ans.push_back(word.substr(0, 1));
        ans.push_back("1");
        if(n == 1) {
            return ans;
        }
        
        for(int i = 1; i < n; i++) {
            int size = ans.size();
            for(int j = 0; j < size; j++) {
                int len = ans[j].size();
                if(isdigit(ans[j][len-1])) {
                    int st = len - 1;
                    while(st >= 0 && isdigit(ans[j][st])) st--;
                    st++;
                    int tmp = stoi(ans[j].substr(st, len - st)) + 1;
                    ans.push_back(ans[j].substr(0, st) + to_string(tmp));
                }
                else {
                    ans.push_back(ans[j] + "1");
                }
                ans[j] += word[i];
            }
        }
        
        return ans;
    }
    };

Log in to reply
 

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