原题链接在这里: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. [SVN] - 使用 TortoiseSVN 进行文件比对非常慢 之 解决

    背景 Windows 10 + TortoiseSVN-v1.9.7使用默认的 TortoiseMerge 进行文件比对,打开速度非常非常慢. 解决 禁用 TortoiseMerge / Settin ...

  2. spring的事务解决方案之@Transactional注解

    首先此注解位于 org.springframework.transaction.annotation 这个包路径下面, 事务有两种类别,一种是编程式事务,另一种是声明式事务,显然此注解是声明式事务,这 ...

  3. 在论坛中出现的比较难的sql问题:30(row_number函数 物料组合问题)

    原文:在论坛中出现的比较难的sql问题:30(row_number函数 物料组合问题) 在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...

  4. Java定时任务工具详解之Timer篇

    Java定时任务调度工具详解 什么是定时任务调度? ◆ 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Java中的定时调度工具? ◆ Timer       ◆Quartz T ...

  5. dede织梦如何去除网站底部的版权信息

    dede织梦如何删除版权powered by?在织梦中,特别是在仿站之中,经常会在首页的底部出现powered by,这是dedecms织梦系统底部自带的官方链接power by等字样,很多新用户想去 ...

  6. 1 spring如何通过组件扫描和自动装配实现自动化的配置

    1 首先将spring依赖的包全部导入 2 建立测试接口 public interface CompactDisc { void play(); } 3 具体的类实现接口 import org.spr ...

  7. day14-python之集合函数字符串格式化

    1.集合 #!/usr/bin/env python # -*- coding:utf-8 -*- # s=set(['alex','alex','sb']) # print(s) # s=set(' ...

  8. Eclipse上安装websphere

    Eclipse上安装websphere 参考:https://blog.csdn.net/qq_26264237/article/details/90107508 安装websphere插件 WebS ...

  9. __init__、__new__方法详解

    __init__详解 class Dog(object): def __init__(self): print('init方法') def __del__(self): print('del方法') ...

  10. [#Linux] CentOS 7 禁用笔记本的触摸板

    安装 xorg-x11-apps yum install xorg-x11-apps 查看对应设备的 id xinput –list 关闭 touchpad xinput set-int-prop 1 ...