LeetCode 784 Letter Case Permutation (Python)

Posted by 小明MaxMing on February 16, 2021

题目

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. You can return the output in any order.

解题思路

从空字符串开始拓展,遍历输入字符串,如果当前字符是字母,则将现在所有的字符串拓展成大小写两个,否则将当前字符添加在后面

代码

class Solution:
    def letterCasePermutation(self, S: str) -> List[str]:
        res = ['']
        for c in S:
            if c.isalpha():
                res = [i + j for i in res for j in [c.upper(), c.lower()]]
            else:
                res = [i + c for i in res]
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅