Leetcode•Jul 09, 2025
Letter Case Permutation
Hazrat Ali
Leetcode
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
/**
* @param {string} S
* @return {string[]}
*/
const letterCasePermutation = S => {
const result = [];
backtracking(S, 0, '', result);
return result;
};
const backtracking = (S, i, solution, result) => {
if (i === S.length) {
result.push(solution);
return;
}
backtracking(S, i + 1, solution + S[i].toLowerCase(), result);
if (/[a-zA-Z]/.test(S[i])) {
backtracking(S, i + 1, solution + S[i].toUpperCase(), result);
}
};