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

最新文章

  1. 致命错误: zlib.h:没有那个文件或目录
  2. 豪斯课堂K先生全套教程淘宝设计美工第一期+第四期教程(无水印)
  3. 58同城高性能移动Push推送平台架构演进之路
  4. android控制系统音量
  5. SAP程序代码中RANGE表的用法禁忌
  6. MongoDB安装与启动
  7. POJ 1564 经典dfs
  8. 《慕客网:IOS-动画入门》学习笔记
  9. 锋利的jQuery-7--$.extend()
  10. linux包之procps之pmap命令
  11. Hive MapJoin
  12. c++在函数后面加const
  13. 让Linux修改IP、DNS等可以更简单
  14. MongoDB基本shell操作
  15. JavaScript基础教程1-20160612
  16. 云计算--网络原理与应用--20171123--网络地址转换NAT
  17. 我的Python之旅第六天--面向对象初识
  18. Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)
  19. MySQL设置远程连接
  20. Redis的appendfsync参数详解

热门文章

  1. golang笔记之DOS篇
  2. Python 基础 常用运算符
  3. CLRS10.2-4练习 - 修改链表查询方法
  4. linux搭建GitLab
  5. pandas-02 Series()和DataFrame()的区别与联系
  6. tf常见的损失函数(LOSS)汇总
  7. github-git clone 下载很慢的问题解决
  8. VSCode 控制台面板输出乱码 字符编码问题 PHP --已解决
  9. Ubuntu 开发环境搭建
  10. Ubuntu安装32位程序兼容包