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 > )
cur += char(count + '');
vr.push_back(cur);
return;
}
//Stop converting the characters into numbers
if(count > )
dfs(str, vr, cur + char(count + '') + str[i], , i + );
else
dfs(str, vr, cur + str[i], , i + );
//Continue converting ....
dfs(str, vr, cur, count + , i + );
return;
}
vector<string> vs(string str) {
vector<string> vResults;
dfs(str, vResults, "", , );
return vResults;
}


[Locked] Generalized Abbreviation的更多相关文章

  1. [LeetCode] Generalized Abbreviation 通用简写

    Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...

  2. LeetCode Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  3. [Swift]LeetCode320. 通用简写 $ Generalized Abbreviation

    Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...

  4. LeetCode 320. Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  5. 【LeetCode】320. Generalized Abbreviation 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  6. 320. Generalized Abbreviation

    首先想到的是DFS,对于每个单词的字母都遍历,比如 spy: 1py,s1y,sp1 然后每个遍历完的单词再DFS..左右有数字就合并比如 1py: 11y=>2py, 1p1 这样.. 但是单 ...

  7. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

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

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  9. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

随机推荐

  1. ActionScript:Resampling PCM data

    使用基于flash的麦克风录音,如果想在获取完PCM采样数据后,通过Sound马上回放,必须经过resampling.(注意:如果录音是采用的44KHz的话,则不需要) 因此,需要as实现一个简便的函 ...

  2. PL/SQL Developer简单使用

    表在以下:

  3. 更新xcode后插件失效问题——不针对特定版本的通用解决方法

    一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...

  4. ETLLib库走读

    以前从来也没想过要去看库里的代码,咱想,那怎么也是很底层的代码,居然发现库里的代码也可以这样写,那就先读懂这个ETLLib(跨windows与Linux平台,封装了一些基本的系统调用函数,然后实现定制 ...

  5. OC 字符和日期的互转

    需要用到NSDateFormatter这个类. 1.字符串转换为日期 [plain]NSDateFormatter* dateFormat = [[NSDateFormatter alloc] ini ...

  6. c#的异或运算符

    int a = 5; int b = 30; Console.WriteLine(a^b); Console.ReadKey();  输出结果是27 这是因为 5的二进制是0000 010130的二进 ...

  7. 用PHP添加购物商品

    <?php session_start(); header ( "Content-type: text/html; charset=UTF-8" ); //设置文件编码格式 ...

  8. html5系列.基础知识

    兼容性问题 创建一个html5页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  9. VS2010 release 和 debug 调试区别

    VC下Debug和Release区别 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下又无法进行调试,于是只能采用printf方式逐步 ...

  10. [R] /usr/share/doc/apache2/README.Debian.gz

    Contents======== Apache2 Configuration under Debian GNU/Linux Files and Directories in '/etc/apache2 ...