my C++ standard backtracking solution


  • 0
    D
    class Solution {
    public:
        vector<string> result;
        vector<string> generateAbbreviations(string word) {
            if (word.empty()) return {""}; // handle [""] case
            generateAbbreviations(word, "", false, 0);
            generateAbbreviations(word, "", true, 0);
            return result;
        }
        
        void generateAbbreviations(string &word, string abbr, bool prev, int start) {
            if (start == word.size()) {
                result.push_back(abbr);
            }
            for (int i = start; i < word.size(); i++) {
                int len = i - start + 1;
                string next = abbr + (prev ? to_string(len) : word.substr(start, len));
                generateAbbreviations(word, next, !prev, i + 1);
            }
        }
    };
    

Log in to reply
 

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