Letter Case Permutation - LeetCode
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.
Example 1: Input: s = “a1b2” Output: [“a1b2”,“a1B2”,“A1b2”,“A1B2”]
- code
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
res = []
def recur(i, sofar):
if i == len(s):
res.append(sofar)
return
if s[i].isdigit():
recur(i+1, sofar + s[i])
else:
recur(i+1, sofar + s[i].upper())
recur(i+1, sofar + s[i].lower())
recur(0, "")
return res
- code
class Solution(object):
def letterCasePermutation(self, S):
def backtrack(sub="", i=0):
if len(sub) == len(S):
res.append(sub)
else:
if S[i].isalpha():
backtrack(sub + S[i].swapcase(), i + 1)
backtrack(sub + S[i], i + 1)
res = []
backtrack()
return res