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 初始化一個(gè)list=[],但由于list中的element是字符串,所以初始化寫(xiě)成['']的形式,代表初始化為以字符串為element的list
3 一個(gè)字符一個(gè)字符地判斷,將此字符前的所有字符的所有permutation都寫(xiě)到res中,當(dāng)判斷下一個(gè)字符時(shí),只需要將之前所有組合+當(dāng)前字符的形式再做一次permutation就可以了
4?if not S: return [''] 也是要返回成這樣才行