[784] Letter Case Permutation
https://leetcode.com/problems/letter-case-permutation/description/
- algorithms
- Easy (53.80%)
- Source Code: 784.letter-case-permutation.py
- Total Accepted: 42.3K
- Total Submissions: 75.7K
- Testcase Example: '"a1b2"'
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 between 1 and 12.
S will consist only of letters or digits.
python
class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
result = []
if not S: return result
lens = len(S)
i = 0
self.enum_every_char(S, i, lens, result, '')
return result
def enum_every_char(self, S, i, lens, result, current):
if i == lens:
result.append(current)
return
else:
if 'A' <= S[i].upper() <= 'Z':
self.enum_every_char(S, i+1, lens, result, current+S[i].lower())
self.enum_every_char(S, i+1, lens, result, current+S[i].upper())
else:
self.enum_every_char(S, i+1, lens, result, current+S[i])