800. Letter Case Permutation
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to generate all possible strings by changing the case of letters in a given input string s. Digits in the string should remain unchanged. The core of the problem is to explore all combinations of letter cases.
Solution
Letter Case Permutation in Python
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
result = []
# Use a list of characters for mutable operations
path = list(s)
def backtrack(index):
# Base case: When index reaches the end, a full permutation is formed.
if index == len(path):
result.append("".join(path))
return
# If the character is a digit, just recurse for the next index.
if path[index].isdigit():
backtrack(index + 1)
# If the character is a letter, explore both cases.
else:
# Branch 1: lowercase
original_char = path[index]
path[index] = original_char.lower()
backtrack(index + 1)
# Branch 2: uppercase
path[index] = original_char.upper()
backtrack(index + 1)
# Backtrack: Restore the character in the path list for previous recursive calls.
path[index] = original_char
backtrack(0)
return result
Letter Case Permutation in Java
class Solution {
public List<String> letterCasePermutation(String s) {
List<String> result = new ArrayList<>();
// Using a char array is efficient as it's mutable.
backtrack(s.toCharArray(), 0, result);
return result;
}
private void backtrack(char[] sChars, int index, List<String> result) {
// Base case: If we have processed all characters, add the permutation to the result.
if (index == sChars.length) {
result.add(new String(sChars));
return;
}
char c = sChars[index];
// If the character is a letter, we have two branches.
if (Character.isLetter(c)) {
// Branch 1: lowercase
sChars[index] = Character.toLowerCase(c);
backtrack(sChars, index + 1, result);
// Branch 2: uppercase
sChars[index] = Character.toUpperCase(c);
backtrack(sChars, index + 1, result);
// Backtrack: Restore the original character to leave the array clean for other branches.
// This is important because the char array is passed by reference.
sChars[index] = c;
} else {
// If it's a digit, just move to the next character.
backtrack(sChars, index + 1, result);
}
}
}
Letter Case Permutation in C++
class Solution {
public:
vector<string> letterCasePermutation(string s) {
vector<string> result;
backtrack(s, 0, result);
return result;
}
private:
void backtrack(string current_s, int index, vector<string>& result) {
// Base case: If we have processed all characters, add the permutation to the result.
if (index == current_s.length()) {
result.push_back(current_s);
return;
}
// Get the character at the current index.
char c = current_s[index];
// If the character is a letter, we have two branches.
if (isalpha(c)) {
// Branch 1: lowercase
current_s[index] = tolower(c);
backtrack(current_s, index + 1, result);
// Branch 2: uppercase
// No need to backtrack the change on current_s[index] because C++
// passes strings by value by default. The change is local to this
// function call and its subsequent recursive calls.
current_s[index] = toupper(c);
backtrack(current_s, index + 1, result);
} else {
// If it's a digit, just move to the next character.
backtrack(current_s, index + 1, result);
}
}
};
Letter Case Permutation in TypeScript
function letterCasePermutation(s: string): string[] {
const result: string[] = [];
// Using an array of characters for mutable operations.
const path = s.split('');
function backtrack(index: number) {
// Base case: If we've processed the whole string, add the current permutation.
if (index === path.length) {
result.push(path.join(''));
return;
}
const char = path[index];
// Regex to check if the character is a letter.
if (/[a-zA-Z]/.test(char)) {
// Branch 1: lowercase
path[index] = char.toLowerCase();
backtrack(index + 1);
// Branch 2: uppercase
path[index] = char.toUpperCase();
backtrack(index + 1);
// Backtrack: Restore the character. This is crucial because `path` is a mutable object
// shared across recursive calls.
path[index] = char;
} else {
// If it's a digit, just move to the next character.
backtrack(index + 1);
}
}
backtrack(0);
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.