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"]
Constraints:
1 <= s.length <= 12
s
consists of lowercase English letters, uppercase English letters, and digits.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find all possible versions of a string where some letters are uppercase and others are lowercase. Brute force means we'll try absolutely every single combination of upper and lowercase letters.
Here's how the algorithm would work step-by-step:
def letter_case_permutation(input_string):
possible_permutations = []
def backtrack(current_string, index):
# Base case: If we've processed all characters, add the permutation
if index == len(input_string):
possible_permutations.append(current_string)
return
current_character = input_string[index]
# If the character is a digit, simply proceed to the next character
if current_character.isdigit():
backtrack(current_string + current_character, index + 1)
else:
# Explore both lowercase and uppercase options
# First convert to lowercase
backtrack(current_string + current_character.lower(), index + 1)
# Then convert to uppercase
backtrack(current_string + current_character.upper(), index + 1)
backtrack("", 0)
return possible_permutations
The trick is to think about building the possible results one character at a time. When we see a letter, we branch out and create two possibilities: one where the letter is uppercase and one where it's lowercase. We continue building on each of these possibilities until we have explored every letter in the input string.
Here's how the algorithm would work step-by-step:
def letter_case_permutation(input_string):
possible_permutations = [""]
for character in input_string:
# Branch to handle upper/lower case for letters
if character.isalpha():
new_permutations = []
for permutation in possible_permutations:
new_permutations.append(permutation + character.lower())
new_permutations.append(permutation + character.upper())
possible_permutations = new_permutations
# Non-letters are added to all existing permutations.
else:
for index in range(len(possible_permutations)):
possible_permutations[index] += character
return possible_permutations
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list immediately, as there are no permutations to generate. |
String with only digits | Return a list containing only the original string, as there are no letters to permute. |
String with only one character (letter) | Return a list containing both the uppercase and lowercase versions of the character. |
String with mixed case letters and digits | The solution should correctly handle both uppercase and lowercase letters and ignore digits. |
Maximum length string (consider recursion depth or memory usage) | The solution should be designed to be space and time efficient and avoid excessive recursion depth for very long strings. |
String with special characters | The provided constraints specify only letters and digits, so special characters should be explicitly handled by either throwing an exception or ignoring them in the permutation process. |
String with all the same letters (e.g., 'aaaa') | The solution should still generate all possible case permutations even with repeated identical letters. |
Case sensitivity differences affecting permutations (e.g. 'aA') | The solution should ensure each letter, regardless of the initial case, can be either uppercase or lowercase in the permutations. |