给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。
示例:
输入: S = “a1b2”
输出: [“a1b2”, “a1B2”, “A1b2”, “A1B2”]
分析:深度优先遍历dfs,依次遍历数组,遇到数字直接往下走,遇到字母,把字母分成大小写字母后继续往下!
python3 代码如下
class Solution:
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
res =[]
pre =''
index =0
self.dfs(pre,S,res,index)
return res
def dfs(self,pre,S,res,index):
if index ==len(S):
res.append(pre)
else:
ch =S[index]
if str.isdigit(ch):
self.dfs(pre+ch,S,res,index+1)
else:
ch =str.lower(ch)
self.dfs(pre+ch,S,res,index+1)
ch =str.upper(ch)
self.dfs(pre+ch,S,res,index+1);