就是枚举出所有情况:

class Solution:
    def letterCasePermutation(self, S):
        permutations = ['']

        for s in S:
            if s.isalpha():
                permutations = [p + letter for p in permutations for letter in
                                [s.lower(), s.upper()]]
            else:
                permutations = [p + s for p in permutations]

也可以使用itertools模块里面的笛卡尔积函数:

class Solution:
    def letterCasePermutation(self, S):
        L = [[i.lower(), i.upper()] if i.isalpha() else i for i in S]
        return [''.join(i) for i in itertools.product(*L)]

 

相关文章:

  • 2021-07-05
  • 2022-12-23
  • 2021-09-16
  • 2021-12-13
  • 2021-07-28
  • 2021-10-23
  • 2021-07-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2021-09-09
  • 2022-01-12
  • 2022-12-23
相关资源
相似解决方案