class Solution:
def expand(self, s: str) -> List[str]:
stack = []
index = 0
container = []
while index < len(s):
if s[index] == "{":
index += 1
while s[index] != '}':
if s[index].isalpha():
stack.append(s[index])
index += 1
container.append(stack)
stack = []
index += 1
else:
container.append([s[index]])
index += 1
self.res = []
self.backtracking(container, self.res, "", 0)
self.res.sort()
return self.res
def backtracking(self, container, res, temp, index):
if index == len(temp) == len(container):
res.append(temp)
return
for i in range(len(container[index])):
temp += container[index][i]
self.backtracking(container, res, temp, index+1)
temp = temp[:-1]
backtracking. 把这个string转化为二维数组之后,就和letter combination(17. Letter Combinations of a Phone Number)是一个套路了。