forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrace Expansion II.py
More file actions
84 lines (75 loc) · 2.95 KB
/
Brace Expansion II.py
File metadata and controls
84 lines (75 loc) · 2.95 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Brace Expansion II
# https://leetcode.com/problems/brace-expansion-ii/
class Solution(object):
def isList(self, expression):
try:
if(expression.isalpha()):
return False
except:
return True
def makeItIntoList(self, expression):
if not (self.isList(expression)):
return [expression]
return expression
def union(self, list1, list2):
return list(set(list1) | set(list2))
def concatinateSet(self, list1, list2):
# we swap lists coz it's a stack and we are popping in reverse order
output = []
for c1 in list2:
for c2 in list1:
output.append(str(c1)+str(c2))
return output
def convertToList(self, expression):
stack = []
output = []
for char in expression:
if(char == '}'):
string = []
first = None
second = None
while(stack[-1] != '{'):
print stack, first, second
if(first == None):
first = self.makeItIntoList(stack.pop())
print first
elif(second == None):
second = stack.pop()
if(second == ','):
second = self.makeItIntoList(stack.pop())
first = self.union(first, second)
else:
second = self.makeItIntoList(second)
first = self.concatinateSet(first, second)
second = None
stack.pop()
stack.append(sorted(first))
else:
if(char != ',' and char != "{"):
# look at previous char
while(stack[-1] != ',' and stack[-1] != '{'):
# if stack top and char are not lists
if(not self.isList(stack[-1]) and not self.isList(char)):
char = stack.pop() + char
else:
# if either one of them is a list then
charList = self.makeItIntoList(char)
top = self.makeItIntoList(stack.pop())
char = self.concatinateSet(charList, top)
print 'char now is', char
stack.append(char)
print stack
def braceExpansionII(self, expression):
array = self.convertToList('{'+expression+'}')
#self.normalizeExpression(array)
"""
:type expression: str
:rtype: List[str]
"""
# Main
obj = Solution()
#obj.braceExpansionII("a{b,c}d")
obj = Solution()
#obj.braceExpansionII("{a,b}{c,{d,e}}")
obj = Solution()
obj.braceExpansionII("{{a,z},a{b,c},{ab,z}}")