File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 2
2
# , l is the length of a word
3
3
# Space: O(p*l)
4
4
5
+ import itertools
6
+
7
+
5
8
class Solution (object ):
9
+ def expand (self , S ): # nested is fine
10
+ """
11
+ :type S: str
12
+ :rtype: List[str]
13
+ """
14
+ def form_words (options ):
15
+ words = list (map ("" .join , itertools .product (* options )))
16
+ words .sort ()
17
+ return words
18
+
19
+ def generate_option (expr , i ):
20
+ option_set = set ()
21
+ while i [0 ] != len (expr ) and expr [i [0 ]] != "}" :
22
+ i [0 ] += 1 # { or ,
23
+ for option in generate_words (expr , i ):
24
+ option_set .add (option )
25
+ i [0 ] += 1 # }
26
+ option = list (option_set )
27
+ option .sort ()
28
+ return option
29
+
30
+ def generate_words (expr , i ):
31
+ options = []
32
+ while i [0 ] != len (expr ) and expr [i [0 ]] not in ",}" :
33
+ tmp = []
34
+ if expr [i [0 ]] not in "{,}" :
35
+ tmp .append (expr [i [0 ]])
36
+ i [0 ] += 1 # a-z
37
+ elif expr [i [0 ]] == "{" :
38
+ tmp = generate_option (expr , i )
39
+ options .append (tmp )
40
+ return form_words (options )
41
+
42
+ return generate_words (S , [0 ])
43
+
44
+
45
+ class Solution2 (object ):
6
46
def expand (self , S ): # nested is fine
7
47
"""
8
48
:type S: str
You can’t perform that action at this time.
0 commit comments