-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbraceExpansionII.py
More file actions
32 lines (30 loc) · 914 Bytes
/
braceExpansionII.py
File metadata and controls
32 lines (30 loc) · 914 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/brace-expansion-ii/
# Author: Miao Zhang
# Date: 2021-04-08
class Solution:
def braceExpansionII(self, expression: str) -> List[str]:
res = []
visited = set()
stack = []
stack.append(expression)
while stack:
s = stack.pop()
if s.find('{') == -1:
if s not in visited:
visited.add(s)
res.append(s)
continue
i, left, right = 0, 0, 0
while s[i] != '}':
if s[i] == '{': left = i
i += 1
right = i
before = s[:left]
after = s[right+1:]
mid = s[left + 1: right]
for c in mid.split(','):
stack.append(before + c + after)
res.sort()
return res