# 784. Letter Case Permutation
Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Example 1:
Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]
Example 2:
Input: s = "3z4"
Output: ["3z4","3Z4"]
# Solution
Approach 1: backtracking.
# Code (Python)
Approach 1:
from itertools import product
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
chars = list(s)
result = []
self._backtrack(0, [], chars, result)
return result
'''
# using python itertools
lst = list(s)
for i in range(len(lst)):
if lst[i].isalpha():
lst[i] = lst[i].lower() + lst[i].upper()
return list(''.join(item) for item in product(*lst))
'''
def _backtrack(self, index, so_far, chars, result):
if index == len(chars):
result.append(''.join(so_far))
return
if not chars[index].isalpha():
self._backtrack(index + 1, so_far + [chars[index]], chars, result)
else:
self._backtrack(index + 1, so_far + [chars[index].lower()], chars, result)
self._backtrack(index + 1, so_far + [chars[index].upper()], chars, result)
Approach 2:
# Code (C++)
Approach 1:
Approach 2: