-
Notifications
You must be signed in to change notification settings - Fork 1
/
_1096_BraceExpansionII.py
38 lines (33 loc) · 1.18 KB
/
_1096_BraceExpansionII.py
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
#-----------------------------------------------------------------------------
# Runtime: 48ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def braceExpansionII(self, expression: str) -> [str]:
stack, union, product = [], [], [""]
for ch in expression:
if ch == '{':
stack.append(union)
stack.append(product)
union, product = [], [""]
elif ch == '}':
pre_product = stack.pop()
pre_union = stack.pop()
for item in product:
union.append(item)
product = []
for str1 in pre_product:
for str2 in union:
product.append(str1 + str2)
union = pre_union
elif ch == ',':
for item in product:
union.append(item)
product = [""]
else:
for i in range(len(product)):
product[i] += ch
for item in product:
union.append(item)
return sorted(set(union))