[Locked] Generalized Abbreviation

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Locked] Generalized Abbreviation相关的知识,希望对你有一定的参考价值。

Write a function to generate the generalized abbreviations of a word.

Example:
Given word = "word", return the following list (order does not matter):

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d","w3", "4"]

分析:

  DFS或者BFS,按照规则将字母替换成数字,注意递归条件和参数的正确性

代码:

void dfs(string &str, vector<string> &vr, string cur, int count, int i) {
    //Condition for ending
    if(i == str.length()) {
        if(count > 0)
            cur += char(count + 0);
        vr.push_back(cur);
        return;
    }
    //Stop converting the characters into numbers
    if(count > 0)
        dfs(str, vr, cur + char(count + 0) + str[i], 0, i + 1);
    else
        dfs(str, vr, cur + str[i], 0, i + 1);
    //Continue converting ....
    dfs(str, vr, cur, count + 1, i + 1);
    return;
}
vector<string> vs(string str) {
    vector<string> vResults;
    dfs(str, vResults, "", 0, 0);
    return vResults;
}

 



以上是关于[Locked] Generalized Abbreviation的主要内容,如果未能解决你的问题,请参考以下文章

Generalized Abbreviation

320. Generalized Abbreviation

320. Generalized Abbreviation

python-子集Generalized Abbreviation(广义缩写)

[LeetCode] Generalized Abbreviation 通用简写

Leetcode 320: Generalized Abbreviation