leetcode:784.字母大小写全排列

给定一个字符串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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值