-
Notifications
You must be signed in to change notification settings - Fork 115
/
1096-BraceExpansionII.cs
57 lines (52 loc) · 1.76 KB
/
1096-BraceExpansionII.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
//-----------------------------------------------------------------------------
// Runtime: 308ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1096_BraceExpansionII
{
public IList<string> BraceExpansionII(string expression)
{
var stack = new Stack<List<string>>();
var union = new List<string>();
var product = new List<string>() { "" };
foreach (var ch in expression)
{
if (ch >= 'a' && ch <= 'z')
{
for (int i = 0; i < product.Count; i++)
product[i] += ch;
}
else if (ch == '{')
{
stack.Push(union);
stack.Push(product);
union = new List<string>();
product = new List<string>() { "" };
}
else if (ch == '}')
{
var pre_product = stack.Pop();
var pre_union = stack.Pop();
union.AddRange(product);
product = new List<string>();
foreach (var str1 in pre_product)
foreach (var str2 in union)
product.Add(str1 + str2);
union = pre_union;
}
else
{
union.AddRange(product);
product = new List<string>() { "" };
}
}
union.AddRange(product);
return union.Distinct().OrderBy(s => s).ToList();
}
}
}