LeetCode 320. Generalized Abbreviation
原题链接在这里: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的更多相关文章
- 【LeetCode】320. Generalized Abbreviation 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 320. Generalized Abbreviation
首先想到的是DFS,对于每个单词的字母都遍历,比如 spy: 1py,s1y,sp1 然后每个遍历完的单词再DFS..左右有数字就合并比如 1py: 11y=>2py, 1p1 这样.. 但是单 ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [LeetCode] Generalized Abbreviation 通用简写
Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...
- LeetCode Generalized Abbreviation
原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...
- [Swift]LeetCode320. 通用简写 $ Generalized Abbreviation
Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...
- Leetcode: Valid Word Abbreviation
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
随机推荐
- Java基础笔试练习(四)
1.编译Java Application 源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为( ). A.java B.class C.html D.exe 答案: B 解析: Java源程序 ...
- (一)SpringBoot Demo之 Hello World
文章目录 最终效果 pom文件编写 编写资源类 编写控制器 运行项目 原文 : https://spring.io/guides/gs/rest-service/ 类型:官网入门指南 要求:JDK1. ...
- English Grammar in Use - Part1 Present and past
Unit 1 Present continuous (I am doing) A) Am/is/are + -ing is the Present continuous. B) I am doing ...
- c++实现的顺序栈
栈是一种运算受限的线性表,是一种先进后出的数据结构,限定只能在一端进行插入和删除操作,允许操作的一端称为栈顶,不允许操作的称为栈底 因此需要的成员变量如下 int *_stack; //指向申请的空间 ...
- hadoop 完全分布式搭建总结
完全分布式--------------- 1.配置文件 [core-site.xml] hdfs 地址 fs.defaultFS=hdfs://s129:8020/ [hdfs-site.xml] 副 ...
- 在vue中使用ElementUI
完整引用ElementUI: 安装:在需要使用到的vue项目目录下,使用npm下载安装: npm/cnpm i element-ui -S/--save <!-- 引入样式 --> < ...
- ALV报表——点击事件(二)
目录 一.ALV点击事件(双击) 一.ALV点击事件(双击) 代码: *Report ZRFI001_XFL_TEST REPORT ZRFI001_XFL_TEST . *定义ALV所需要用到的类型 ...
- 十分钟快速创建 Spring Cloud 项目
一般来说,Intelij IDEA 可以通过 Maven Archetype 来快速生成Maven项目,其实 IDEA 集成了 Spring 官方提供的 Spring Initializr,可以非常方 ...
- 写一个RD一般需要多久?在迭代中新增的需求如何处理?如何做好项目管理?
最近总是有些初入行的小伙们,经常会问我一些他们在工作工作的中疑问,我今天挑选出几个比较典型的问题和大家一起来讨论下. 问题如下: 写一个PRD一般需要多久? 在迭代中新增的需求如何处理? 如何做好项目 ...
- Linux:定时任务crond服务
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...