-
Notifications
You must be signed in to change notification settings - Fork 112
/
1087-BraceExpansion.cs
61 lines (55 loc) · 1.69 KB
/
1087-BraceExpansion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//-----------------------------------------------------------------------------
// Runtime: 284ms
// Memory Usage: 33.4 MB
// Link: https://leetcode.com/submissions/detail/369751390/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _1087_BraceExpansion
{
public string[] Expand(string S)
{
var map = new List<IList<char>>();
var inBrace = false;
var chars = new List<char>();
foreach (var ch in S)
{
if (ch == '{')
inBrace = true;
else if (ch == '}')
{
inBrace = false;
chars.Sort();
map.Add(chars);
chars = new List<char>();
}
else if (inBrace)
{
if (ch != ',')
chars.Add(ch);
}
else
map.Add(new List<char>() { ch });
}
var result = new List<string>();
DFS(map, 0, new StringBuilder(), result);
return result.ToArray();
}
private void DFS(List<IList<char>> map, int index, StringBuilder sb, List<string> result)
{
if (index == map.Count)
{
result.Add(sb.ToString());
return;
}
foreach (var ch in map[index])
{
sb.Append(ch);
DFS(map, index + 1, sb, result);
sb.Remove(sb.Length - 1, 1);
}
}
}
}