[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;
}


时间: 2024-11-03 05:43:09

[Locked] Generalized Abbreviation的相关文章

[LeetCode] 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", &qu

Leetcode 320: 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", &qu

【python-子集】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”,

[LeetCode]Generalized Abbreviation

感觉我的方法逻辑不够清晰 public class Solution { private List<String> result; public List<String> generateAbbreviations(String word) { result = new ArrayList<String>(); helper(word, -2, 0); result.add(word); return result; } public void helper(Strin

Generalized Abbreviation

1 public class Solution { 2 public List<String> generateAbbreviations(String word) { 3 List<String> result = new ArrayList<>(); 4 5 getAbb(word, 0, 0, "", result); 6 return result; 7 } 8 9 private void getAbb(String word, int i

320. Generalized Abbreviation

这道题没有做出来不应该. 显然是backtracking. 分两种情况: 一种是到当前位置的字母重新开始计数 一种是继续前一个字母的计数 需要注意的是,如果cnt==0是不要加到结果String里的 1 public List<String> generateAbbreviations(String word) { 2 List<String> res = new ArrayList<String>(); 3 if(word == null) { 4 return re

【Todo】所有Locked的题目的分析解答

下面这个链接有比较全的leetcode题目包括锁的 http://www.cnblogs.com/grandyang/p/4606334.html https://leetcode.com/problems/binary-tree-upside-down/ http://blog.csdn.net/xudli/article/details/42362441 https://leetcode.com/problems/read-n-characters-given-read4/ http://b

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact