# 784. Letter Case Permutation (easy) ###### tags: `online_judge`, `python3` 熱身 easy 題目。各個字母都有大、小寫兩種可能。以 recursive 展開即可,以本題的複雜度來說,改用迴圈也不是不行。只是...我懶。 --- ### 題目 ``` 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. Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4" Output: ["3z4", "3Z4"] Input: S = "12345" Output: ["12345"] Note: S will be a string with length between 1 and 12. S will consist only of letters or digits. ``` --- ### 解答 ```python= class Solution: ans = [] def letterCasePermutation(self, S: str) -> List[str]: self.ans = [] self.dfs(S,0) return self.ans def dfs(self, input,start_index) -> List[str]: for i in range(start_index,len(input)): if input[i].isalpha(): input = input[:i] + input[i].lower() + input[i+1:] self.dfs(input,i+1) input = input[:i] + input[i].upper() + input[i+1:] self.dfs(input,i+1) return self.ans.append(input) return ``` --- ### 成績 ![](https://i.imgur.com/lt2sF5n.png) ---