LeetCode 1087. Brace Expansion
原题链接在这里: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 <= S.length <= 50- There are no nested curly brackets.
- 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);
}
}
}
LeetCode 1087. Brace Expansion的更多相关文章
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Recursive-Brace Expansion II
2019-11-26 11:05:10 1096. Brace Expansion II 问题描述: 问题求解: 经典的字符串扩展问题. 一般来说这种问题有两种解法,一个是采用stack,一个是采用r ...
- 前端实用程序包utils - 开发工作流(一)
写在前面 早年间有幸在Raychee哥门下当小弟,学到两把刷子.在编程路上,他的很多思想深深影响了我,比如笔者今天要分享的主题.在程序开发中,有个utils包,叫做实用程序包,程序员们会把项目中通用的 ...
- SH Script Grammar
http://linux.about.com/library/cmd/blcmdl1_sh.htm http://pubs.opengroup.org/onlinepubs/9699919799/ut ...
- Here String 中不该进行分词
我们知道,在 Shell 中,一个变量在被展开后,如果它没有被双引号包围起来,那么它展开后的值还会进行一次分词(word splitting,或者叫拆词,分词这个术语已经被搜索引擎相关技术占用了)操作 ...
- (转载)Bash 中的特殊字符大全
转自:https://linux.cn/article-5657-1.html Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是she ...
- 学习bash
工作8年,前6年基本是Windows环境下,也就是个鼠标党:两年前换工作开始用linux,也就开始了领略了命令行的强大,无论是直接在命令行组合命令,也还写几行简单的shell脚本,其能完成的功能往往令 ...
- Shell-bash中特殊字符汇总[转]
转自http://www.linuxidc.com/Linux/2015-08/121217.htm 首先举例一个bash脚本 #!/bin/bash file=$1 files=`find / -n ...
- SHELL 八大扩展
最近在梳理bash知识的的过程中,有幸阅读了man bash文档,一时间犹如醍醐灌顶一般,很多当初不明白的地方都豁然开朗,现在就其中的一点做一分享,同时也为man bash做一下广告,当你面对bash ...
随机推荐
- 【Python爬虫案例学习】python爬取淘宝里的手机报价并以价格排序
第一步: 先分析这个url,"?"后面的都是它的关键字,requests中get函数的关键字的参数是params,post函数的关键字参数是data, 关键字用字典的形式传进去,这 ...
- day09——初识函数
day09 函数的定义 # len() s = 'alexdsb' count = 0 for i in s: count += 1 print(count) s = [1,2,23,3,4,5,6] ...
- 关于vuecli的一些问题
在vue打包之后,我们引入的css路径和js路径会变成绝对路径 需要在vue.config.js里面设置publicpath为"./" 同时在做前后端分离开发时,我们通常会用到ax ...
- CF468C Hack It! 构造
传送门 让人觉得脑子不够用的构造 考虑对于一个区间\([l,r]\)如何让它调整使得最后的结果恰好加上\(1\). 注意到对于一个\(<10^{18}\)的数\(x\),\(f(x+10^{18 ...
- Maven 初学+http://mvnrepository.com/
了解 maven是一款服务于java平台的自动化构建工具(项目管理工具) 构建:全方位.多角度.深层次地建立 项目构建是一个项目从:源代码.编译.测试.打包.部署.运行的过程 用来解决团队开发遇到的问 ...
- K-匿名算法研究
12月的最后几天,研究了下k匿名算法,在这里总结下. 提出背景 Internet 技术.大容量存储技术的迅猛发 展以及数据共享范围的逐步扩大,数据的自动采集 和发布越来越频繁,信息共享较以前来得更为容 ...
- node.js数据库操作
node 中使用mysql const http = require('http'); const mysql = require('mysql'); const url = require('url ...
- webpack+vue-cil跨域配置接口地址代理
在vue项目开发的时候,接口联调的时候一般都是同域名下,且不存在跨域的情况下进行接口联调,但是当我们现在使用vue-cli进行项目打包的时候,我们在本地启动服务器后,比如本地开发服务下是 http:/ ...
- 理解 Node.js 中 Stream(流)
Stream(流) 是 Node.js 中处理流式数据的抽象接口. stream 模块用于构建实现了流接口的对象. Node.js 提供了多种流对象. 例如,对 HTTP 服务器的request请求和 ...
- 初次尝试vue脚手架
1.第一步首先安装NodeJs ,从nodejs 官网去down,然后安装 安装完成后,我安装了GIT 自己从官网去下载进行安装 2.检查安装是否成功,windows+r -> cmd,输入 ...