Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.
class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
if not S: return ['']
res = ['']
for s in S:
if s.isalpha():
res = [i+j for i in res for j in [s.upper(), s.lower()]]
else:
res = [i+s for i in res]
return res
1 判断是否是字母,用str.isalpha()
2 初始化一个list=[],但由于list中的element是字符串,所以初始化写成['']的形式,代表初始化为以字符串为element的list
3 一个字符一个字符地判断,将此字符前的所有字符的所有permutation都写到res中,当判断下一个字符时,只需要将之前所有组合+当前字符的形式再做一次permutation就可以了
4 if not S: return [''] 也是要返回成这样才行