原题链接在这里:https://leetcode.com/problems/brace-expansion/

题目:

A string S represents a list of words.

Each letter in the word has 1 or more options.  If there is one option, the letter is represented as is.  If there is more than one option, then curly braces delimit the options.  For example, "{a,b,c}" represents options ["a", "b", "c"].

For example, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].

Return all words that can be formed in this manner, in lexicographical order.

Example 1:

Input: "{a,b}c{d,e}f"
Output: ["acdf","acef","bcdf","bcef"]

Example 2:

Input: "abcd"
Output: ["abcd"]

Note:

  1. 1 <= S.length <= 50
  2. There are no nested curly brackets.
  3. All characters inside a pair of consecutive opening and ending curly brackets are different.

题解:

If there is curly braces, all the chars in it could be candidate.

Starting from index 0. Do DFS, DFS state needs orginal string, current index, current StringBuilder and res collection.

If current index i points to '{', then find next index j points to '}', for each of candidate inside brances, append it to StringBuilder and continue DFS at index j+1. After DFS, do backtracking.

If current index i points to char, append it to StringBuilder and continue DFS at index i+1. After DFS, do bracktracking.

When current index points to the end of string, add copy of StringBuilder to res collection.

Time Complexity: exponential.

Space: O(n). n = S.length(). stack space.

AC Java:

 class Solution {
public String[] expand(String S) {
if(S == null || S.length() == 0){
return new String[0];
} List<String> res = new ArrayList<String>();
dfs(S, 0, new StringBuilder(), res);
Collections.sort(res);
return res.toArray(new String[0]);
} private void dfs(String s, int i, StringBuilder sb, List<String> res){
if(i >= s.length()){
res.add(sb.toString());
return;
} if(s.charAt(i) == '{'){
int j = i+1;
while(j<s.length() && s.charAt(j)!='}'){
j++;
} String [] candidates = s.substring(i+1, j).split(",");
for(String candidate : candidates){
sb.append(candidate);
dfs(s, j+1, sb, res);
sb.deleteCharAt(sb.length()-1);
}
}else{
sb.append(s.charAt(i));
dfs(s, i+1, sb, res);
sb.deleteCharAt(sb.length()-1);
}
}
}

类似Decode String.

LeetCode 1087. Brace Expansion的更多相关文章

  1. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  2. Recursive-Brace Expansion II

    2019-11-26 11:05:10 1096. Brace Expansion II 问题描述: 问题求解: 经典的字符串扩展问题. 一般来说这种问题有两种解法,一个是采用stack,一个是采用r ...

  3. 前端实用程序包utils - 开发工作流(一)

    写在前面 早年间有幸在Raychee哥门下当小弟,学到两把刷子.在编程路上,他的很多思想深深影响了我,比如笔者今天要分享的主题.在程序开发中,有个utils包,叫做实用程序包,程序员们会把项目中通用的 ...

  4. SH Script Grammar

    http://linux.about.com/library/cmd/blcmdl1_sh.htm http://pubs.opengroup.org/onlinepubs/9699919799/ut ...

  5. Here String 中不该进行分词

    我们知道,在 Shell 中,一个变量在被展开后,如果它没有被双引号包围起来,那么它展开后的值还会进行一次分词(word splitting,或者叫拆词,分词这个术语已经被搜索引擎相关技术占用了)操作 ...

  6. (转载)Bash 中的特殊字符大全

    转自:https://linux.cn/article-5657-1.html Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是she ...

  7. 学习bash

    工作8年,前6年基本是Windows环境下,也就是个鼠标党:两年前换工作开始用linux,也就开始了领略了命令行的强大,无论是直接在命令行组合命令,也还写几行简单的shell脚本,其能完成的功能往往令 ...

  8. Shell-bash中特殊字符汇总[转]

    转自http://www.linuxidc.com/Linux/2015-08/121217.htm 首先举例一个bash脚本 #!/bin/bash file=$1 files=`find / -n ...

  9. SHELL 八大扩展

    最近在梳理bash知识的的过程中,有幸阅读了man bash文档,一时间犹如醍醐灌顶一般,很多当初不明白的地方都豁然开朗,现在就其中的一点做一分享,同时也为man bash做一下广告,当你面对bash ...

随机推荐

  1. 23 Collection集合常用方法讲解

    本文讲讲几个Collection的常用方法,这些方法在它的子类中也是很常用的,因此这里先拿出来单独讲解,以后它的子类中的这些方法就不再重复讲解. 几个常用方法: add() 添加一个元素 size() ...

  2. HTML文件直接在浏览器打开和本地服务器localhost打开有什么区别?

    最直接的区别,很容易注意到,一个是file协议,另一个是http协议. file协议更多的是将该请求视为一个本地资源访问请求,和你使用资源管理器打开是一样的,是纯粹的请求本地文件. http请求方式则 ...

  3. 20、Outer Apply 和 Cross Apply

    1.場合 select...caseが複雑の時 2.運用方法 SELECT * FROM stu CROSS APPLY ( --like inner join * FROM score WHERE ...

  4. 阿里巴巴 Java 开发手册(四): OOP 规约

    . [强制]避免通过一个类的对象引用访问此类的静态变量或静态方法,无谓增加编译器解析成 本,直接用类名来访问即可. 2. [强制]所有的覆写方法,必须加@Override 注解. 说明:getObje ...

  5. 【C#】 获取计算机的硬件信息

    添加引用:System.Management /// <summary> /// 获取本机的MAC地址 /// </summary> /// <returns>&l ...

  6. python class 中__next__用法

    class A(): def __init__(self,b): self.b=b # def __iter__(self): # 这个函数可以用,表示迭代标志,但也可以省略 # return sel ...

  7. 30个关于Shell脚本的经典案例(下)

    本文目录 21.从FTP服务器下载文件 22.连续输入5个100以内的数字,统计和.最小和最大 23.将结果分别赋值给变量 24.批量修改文件名 25.统计当前目录中以.html结尾的文件总大 26. ...

  8. 【i.MX6UL/i.MX6ULL开发常见问题】单独编译内核,uboot生成很多文件,具体用哪一个?

    [i.MX6UL/i.MX6ULL开发常见问题]2.3单独编译内核,uboot生成很多文件,具体用哪一个? 答:内核编译出来的文件是~/MYiR-imx-Linux/arch/arm/boot/目录下 ...

  9. Python 第三方日志框架loguru使用

    解决中文乱码问题 项目地址 github: https://github.com/Delgan/loguru 文档:https://loguru.readthedocs.io/en/stable/in ...

  10. 手写MVC框架(二)-代码实现和使用示例

    --------上一篇:手写MVC框架(一)-再出发----- 背景 书接上文,之前整理了实现MVC框架需要写哪些东西.这周粗看了一下,感觉也没多少工作量,所以就计划一天时间来完成.周末的时间,哪会那 ...