simple DFS sln


  • 0
    M
    vector<string> generateAbbreviations(string word) {
        vector<string> res;
        generateAbbr(word, 0, "", res);
        return res;
    }
    
    void generateAbbr(const string& word, int startPos, string preStr, vector<string>& res)
    {
        if (startPos == word.length())
        {
            res.push_back(preStr);
            return;
        }
        
        generateAbbr(word, startPos + 1, preStr + word[startPos], res);
        
        for (int i = startPos + 1; i < word.length(); i++)
        {
            generateAbbr(word, i + 1, preStr + to_string(i - startPos) + word[i], res);
        }
        generateAbbr(word, word.length(), preStr + to_string(word.length() - startPos), res);
        
    }

Log in to reply
 

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