Share my CPP recursive Solution


  • 0
    O
    class Solution {
    public:
        void go(string &word, vector<string> &res, string &path, int pos, bool flag) {
            if (pos == word.size()) {
                res.push_back(path);
            } else {
                path.push_back(word[pos]);
                go(word, res, path, pos + 1, true);
                path.pop_back();
                if (flag == false) return;
                for (int i = 1; i <= word.size() - pos; i++) {
                    int Nt = path.size();
                    path += to_string(i);
                    go(word, res, path, pos + i, false);
                    path = path.substr(0, Nt);
                }
            }
        }
    
        vector<string> generateAbbreviations(string word) {
            vector<string> ans;
            string path;
            go(word, ans, path, 0, true);
            return ans;
        }
    };

Log in to reply
 

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