784. 字母大小写全排列



原串里有数字、也有字母,所以每选一个字符要加以判断,要是字母则有两种选择,数字就一种。

还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html

class Solution(object):
    def __init__(self):
        self.res = []

    def letterCasePermutation(self, S):
        """
        :type S: str
        :rtype: List[str]
        """
        # 特判
        if not S:
            return []
        # 将字符串转成list类型
        lists = list(S)
        # 去原串长度
        n = len(lists)
        self.dfs(lists, 0, n, [])
        return self.res

    def dfs(self, string, cur, n, temp):
        # 定义递归出口
        if len(temp) == len(string):
            self.res.append("".join(temp))
            return
        # 数字直接加
        if string[cur].isdigit():
            self.dfs(string, cur + 1, n, temp + [string[cur]])
        # 小写字母
        elif string[cur].islower():
            self.dfs(string, cur + 1, n, temp + [string[cur]])
            self.dfs(string, cur + 1, n, temp + [string[cur].upper()])
        # 大写字母
        elif string[cur].isupper():
            self.dfs(string, cur + 1, n, temp + [string[cur]])
            self.dfs(string, cur + 1, n, temp + [string[cur].lower()])
原文地址:https://www.cnblogs.com/panweiwei/p/14025298.html