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 ...
随机推荐
- Python3之字符串格式化format函数详解(下)
格式限定符 format通过丰富的的“格式限定符”(语法是 {}中带:号)对需要格式的内容完成更加详细的制定. 进制转换 我们可以再限定符中制定不同的字符对数字进行进制转换的格式化,进制对应的表格: ...
- C++ 计算定积分、不定积分、蒙特卡洛积分法
封装成了一个类,头文件和源文件如下: integral.h #pragma once //Microsoft Visual Studio 2015 Enterprise #include <io ...
- Word 文档内超级链接跳转到书签
1. 前言 在Word文档内如何实现一些跳转的超链接呢?Word中,一些外部链接,我们通常叫作超链接,内部链接我们可以叫书签.如何在文档中如何使用书签,跳转到指定位置? 这里我在网上随便找了一份模拟试 ...
- hdu 5418 题解
第一眼看到这题,哇,这不是我刚做完的题吗?大水题!然后 这题表面很水,实际上有点坑. 题意 求经过 $ 1 - n $(不能遗漏) 并且回到 $ 1 $ 的最短路. 在看这题之前我们可以来看下这题 最 ...
- flannel overlay网络浅析
Flannel基于UDP的网络实现 container-1的route表信息如下(b1): default via 100.96.1.1 dev eth0 100.96.1.0/24 dev eth0 ...
- 海思HI35xx平台软件开发快速入门之H264解码实例学习
ref :https://blog.csdn.net/wytzsjzly/article/details/82500277 前言 H264视频编码技术诞生于2003年,至今已有十余载,技术相当成熟 ...
- Codeforces Round #576 (Div. 1) 简要题解 (CDEF)
1198 C Matching vs Independent Set 大意: 给定$3n$个点的无向图, 求构造$n$条边的匹配, 或$n$个点的独立集. 假设已经构造出$x$条边的匹配, 那么剩余$ ...
- java实现HTTP请求 HttpUtil
示例: package com.sensor.utils; import java.net.HttpURLConnection; import java.net.URL; public class H ...
- NOPI 读与写
Excel读取和写入的完整代码using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using Sys ...
- MVC模式下unity配置,报错“No connection string named '**Context' could be found in the application config file”
写在前面: 第一次配置时好好的,后来第二次改到MVC模式,把依赖注入写成字典的单例模式时,由于新建的ORM(数据库映射模型EF),怎么弄都不用,一直报错"No connection str ...