题目描述:
LeetCode 784. Letter Case Permutation
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.
Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4" Output: ["3z4", "3Z4"] Input: S = "12345" Output: ["12345"]
Note:
S
will be a string with length at most12
.S
will consist only of letters or digits.
题目大意:
给定字符串S,将其中的字母分别转化为大、小写,求所有组合。
解题思路:
递归(Recursion)
Python代码:
class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
if not S: return [S]
rest = self.letterCasePermutation(S[1:])
if S[0].isalpha():
return [S[0].lower() + s for s in rest] + [S[0].upper() + s for s in rest]
return [S[0] + s for s in rest]
本文链接:http://bookshadow.com/weblog/2018/02/18/leetcode-letter-case-permutation/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。