原题链接在这里:https://leetcode.com/problems/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"]

题解:

For DFS, the state needs word, current index, current string, res and count of abbreviated char.

对于当前char有两个选择: 第一缩写当前char, count+1, index+1. 第二不缩写当前char, 先append 非零的count再append当前char, count回零, index+1.

Backtracking时因为直接用string, 都是copy所以不担心.

Time Complexity: exponential. 在每一步递归时,有两个branches. 递归树全部展开会有2^n个叶子. 每一个叶子都相当于用了O(n)时长, 把StringBuilder变成String就用时O(n). n = word.length().

Space: O(n). n层stack, char array size也是n. regardless res.

AC Java:

 class Solution {
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<String>();
if(word == null || word.length() == 0){
res.add("");
return res;
} dfs(word, 0, 0, "", res);
return res;
} private void dfs(String word, int i, int count, String cur, List<String> res){
if(i == word.length()){
if(count != 0){
cur += count;
} res.add(cur);
return;
} dfs(word, i+1, count+1, cur, res); if(count != 0){
cur += count;
} cur += word.charAt(i);
dfs(word, i+1, 0, cur, res);
}
}

Bit Manipulation 方法是用binary表示word的每一位. 0代表不缩写当前char, 1代表缩写当前char.

word 若是用 0011表示就是不缩写wo, 缩写rd, 最后是wo2.

对于word的所有binary表示, 也就是0000到1111走一遍,每个binary变成string.

Time Complexity: O(n*2^n). n = word.length(), 一共有O(2^n)个binary表示. 每个binary变成string用时O(n).

Space: O(n), regardless res.

AC Java:

 public class Solution {
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<String>();
if(word == null){
return res;
}
int n = 1<<word.length();
char [] s = word.toCharArray();
for(int i = 0; i<n; i++){
res.add(abbr(i, s));
}
return res;
} private String abbr(int num, char [] s){
StringBuilder sb = new StringBuilder();
int count = 0;
for(int i = 0; i<s.length; i++, num>>=1){
// 0 表示不缩写当前char, 1 表示缩写当前char
if((num & 1) == 0){
// 先加上之前的非零count, 再把count清零
if(count != 0){
sb.append(count);
count = 0;
}
sb.append(s[i]);
}else{
count++;
}
}
//最后的非零count不要忘记加进sb中
if(count != 0){
sb.append(count);
}
return sb.toString();
}
}

LeetCode 320. Generalized Abbreviation的更多相关文章

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

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

  2. 320. Generalized Abbreviation

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

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

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

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

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

  5. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  6. [LeetCode] Generalized Abbreviation 通用简写

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

  7. LeetCode Generalized Abbreviation

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

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

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

  9. Leetcode: Valid Word Abbreviation

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

随机推荐

  1. [转帖]年经贴: ARM将为苹果开发高性能CPU核心 取代笔记本x86处理器?

    ARM将为苹果开发高性能CPU核心 取代笔记本x86处理器? https://www.cnbeta.com/articles/tech/899421.htm . 之前苹果的哥们说过 谁特别在意自己的软 ...

  2. Prometheus入门到放弃(1)之Prometheus安装部署

    规划: IP 角色 版本 10.10.0.13 prometheus-server 2.10 10.10.0.11 node_exporter 0.18.1 10.10.0.12 node_expor ...

  3. WUSTOJ 1335: Similar Word(Java)

    题目链接:1335: Similar Word Description It was a crummy day for Lur. He failed to pass to the CET-6 (Col ...

  4. 十九、eMMC驱动框架分析

    一.MMC简介 eMMC在封装中集成了一个控制器,提供标准接口并管理Nand Flash,使得手机厂商就能专注于产品开发的其它部分,并缩短向市场推出产品的时间. 对于我们来说,eMMC就是在Nand ...

  5. 『Python基础』第8节:格式化输出

    现在有一个需求, 询问用户的姓名, 年龄, 工作, 爱好, 然后打印成以下格式 ************ info of Conan ************ name: Conan age: 23 ...

  6. Task 开始 停止

    注意点:需要将每个线程的 MemoryCacheManager 保存,这里我保存在缓存中,需要取消时根据缓存key值取出 MemoryCacheManager //开始Task1 private vo ...

  7. Linux入职基础-1.1_国内开源的主要镜像站

    Linux入职基础-1.1_国内开源的主要镜像站 东北地区: 东北大学  http://mirror.neu.edu.cn 大连理工大学  http://mirror.dlut.edu.cn 大连东软 ...

  8. nodejs中使用mongodb

    /** * 使用mongodb存储数据 * 1 首先安装mongodb nodejs插件 npm install mongodb --save-dev * 2 安装express (非必须) * * ...

  9. vue组件上动态添加和删除属性

    1.vue组件上动态添加和删除属性 // 添加 this.$set(this.obj, 'propName', val) // 删除 this.$delete(this.obj, 'propName' ...

  10. java中级面试题

    1.Java中堆和栈有什么不同? 每个线程都有自己的栈内存,用于存储本地变量,方法参数和栈调用,一个线程中存储的变量对其它线程是不可见的.而堆是所有线程共享的一片公用内存区域.对象都在堆里创建,为了提 ...