forked from alqamahjsr/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1087_Brace_Expansion.py
44 lines (37 loc) · 1.21 KB
/
1087_Brace_Expansion.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
39
40
41
42
43
44
class Solution(object):
def expand(self, S):
"""
:type S: str
:rtype: List[str]
"""
wordList = []
allGeneratedWords = []
self.getWordList(S, wordList)
self.generateWords(wordList, "", allGeneratedWords)
return sorted(allGeneratedWords)
def generateWords(self, wordList, runningWord, allGeneratedWords):
if not wordList:
allGeneratedWords.append(runningWord)
return
word = wordList[0]
for j in range(len(word)):
char = word[j]
runningWord += char
self.generateWords(wordList[1:], runningWord, allGeneratedWords)
runningWord = runningWord[:len(runningWord) - 1]
def getWordList(self, S, wordList):
start = S.find('{')
end = S.find('}')
if start >= 0:
if start > 0:
wordList.append(S[0:start].split(','))
wordList.append(S[start + 1:end].split(','))
self.getWordList(S[end+1:], wordList)
else:
if S:
wordList.append(S.split(','))
return
sol = Solution()
input = "{a,b}{z,x,y}"
output = sol.expand(input)
print('Res: ', output)