原题链接在这里: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:

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

Example 2:

  1. Input: "abcd"
  2. 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:

  1. class Solution {
  2. public String[] expand(String S) {
  3. if(S == null || S.length() == 0){
  4. return new String[0];
  5. }
  6.  
  7. List<String> res = new ArrayList<String>();
  8. dfs(S, 0, new StringBuilder(), res);
  9. Collections.sort(res);
  10. return res.toArray(new String[0]);
  11. }
  12.  
  13. private void dfs(String s, int i, StringBuilder sb, List<String> res){
  14. if(i >= s.length()){
  15. res.add(sb.toString());
  16. return;
  17. }
  18.  
  19. if(s.charAt(i) == '{'){
  20. int j = i+1;
  21. while(j<s.length() && s.charAt(j)!='}'){
  22. j++;
  23. }
  24.  
  25. String [] candidates = s.substring(i+1, j).split(",");
  26. for(String candidate : candidates){
  27. sb.append(candidate);
  28. dfs(s, j+1, sb, res);
  29. sb.deleteCharAt(sb.length()-1);
  30. }
  31. }else{
  32. sb.append(s.charAt(i));
  33. dfs(s, i+1, sb, res);
  34. sb.deleteCharAt(sb.length()-1);
  35. }
  36. }
  37. }

类似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. 用python批量添加保护站点

    最近在测试的过程中,由于一个bug的复现需要添加1600个保护站点,手工添加谁知到要何年何月,因此想到了用python进行自动化批量添加保护站点! 具体代码如下: #!/usr/bin/env pyt ...

  2. Python入门 常量 注释 基础数据类型 用户输入 流程控制

    Python入门 一.常量 在Python中,不像其他语言有绝对的常量,修改会报错,在Python中有个约定俗成的规定--常量就是将变量名大写. 尽量保持不更改的一种量 , 这个常量有是干什么的呢 其 ...

  3. Robot Arms AtCoder - 4432 (构造)

    大意: 给定平面上$n$个点$(x_i,y_i)$. 要求构造一个序列$d$, $d_i$表示每步走的距离, 再构造$n$个命令串, 要求从原点出发按照第$i$个命令走, 走完恰好到达$(x_i,y_ ...

  4. MySql5.7 json查询

    create table t1(name json); insert into t1 values(’ { “hello”: “song”, “num”: 111, “obj”: { “who”: “ ...

  5. 【Maven基础入门】01 Maven的安装与环境变量的配置

    写在前面: Mavne,作为一个优秀的项目构建工具,虽说我们平时在使用的时候或多或少的会使用到它,但了解仅限于它能构建项目,然后其他的就不知道了. 以及仓库.POM父类文件.等等. 工欲善其事,必先利 ...

  6. Hive学习笔记(三)—— 数据类型

    Hive的基本使用(一)--数据类型 1. Hive的基本数据类型 Hive数据类型 Java数据类型 长度 例子 TINYINT byte 1byte有符号整数 20 SMALINT short 2 ...

  7. cas sso docker部署service

    cas协议: 1. 拉取镜像 docker pull apereo/cas:${tag} 2. 启动容器 docker run --name cas -p : -p : apereo/cas:v5.3 ...

  8. linux 安装Python3.6

    1.安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel ...

  9. python安装和pycharm安装与笔记

    目录 计算机的基础知识 python安装和使用 pycharm安装和使用 [TOC] 计算机的基础知识 计算机是由什么组成的 cpu-----大脑 主板----身体 电源----心脏 内存----临时 ...

  10. shell编程必须要掌握的命令-xargs

    一,说xargs命令前,说一下什么是shell编程 什么是shell编程呢,说白了就是按一定的规则把各种命令组织起来,完成一定的事情.纯属个人理解,哈哈.不管是交互式的shell,还是非交互的shel ...