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:
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 20 if(s.charAt(i) == '{'){ 21 int j = i+1; 22 while(j<s.length() && s.charAt(j)!='}'){ 23 j++; 24 } 25 26 String [] candidates = s.substring(i+1, j).split(","); 27 for(String candidate : candidates){ 28 sb.append(candidate); 29 dfs(s, j+1, sb, res); 30 sb.deleteCharAt(sb.length()-1); 31 } 32 }else{ 33 sb.append(s.charAt(i)); 34 dfs(s, i+1, sb, res); 35 sb.deleteCharAt(sb.length()-1); 36 } 37 } 38 }
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11791503.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [译]Buidler 新手教程
- tf.train.shuffle_batch函数解析
- 网页小图标Favicon
- ASP.net Core MVC项目给js文件添加版本号
- 多目标优化非支配关系实现
- linux之shell
- linux下的定时任务处理
- 10 个用纯 Javascript 实现的好用插件
- Tensorflow ActiveFunction激活函数解析
- C++核心准则Con.1:默认情况下使对象不可修改
- 进化算法个体和指标的一致性
- matlab代码折叠与分节注释
- Flask从零到一 1 | 虚拟环境和第一个flask程序
- 按照元素指定条件筛选结构体数组
- 多目标优化按支配关系分层实现