forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrace Expansion.py
More file actions
39 lines (35 loc) · 1.14 KB
/
Brace Expansion.py
File metadata and controls
39 lines (35 loc) · 1.14 KB
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
# Brace Expansion
# https://leetcode.com/problems/brace-expansion/
class Solution(object):
def logic(self, array, index, output, stringList):
if(index == len(array)):
stringList.append(output)
return
for char in array[index].split(','):
self.logic(array, index+1, output+char, stringList)
def expand(self, S):
array = []
for splitLetters in S.split("{"):
array = array + splitLetters.split("}")
# remove empty strings from array
new_array = []
for char in array:
if(char != ''):
if(len(char.split(',')) > 1):
sortedChar = [x for x in sorted(char) if x!=',']
char = ','.join(sortedChar)
new_array.append(char)
stringList = []
self.logic(new_array, 0, "", stringList)
print stringList
"""
:type S: str
:rtype: List[str]
"""
# Main
obj = Solution()
obj.expand("{a,b}c{d,e}f")
obj = Solution()
obj.expand("abcd")
obj = Solution()
obj.expand("{a,b}{z,x,y}")