Missing undo step on backtrack
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.
Move from brute-force thinking to an efficient approach using backtracking strategy.
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 <= 12s consists of lowercase English letters, uppercase English letters, and digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Backtracking · Bit Manipulation
"a1b2"
"3z4"
subsets)brace-expansion)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #784: Letter Case Permutation
class Solution {
private List<String> ans = new ArrayList<>();
private char[] t;
public List<String> letterCasePermutation(String s) {
t = s.toCharArray();
dfs(0);
return ans;
}
private void dfs(int i) {
if (i >= t.length) {
ans.add(new String(t));
return;
}
dfs(i + 1);
if (Character.isLetter(t[i])) {
t[i] ^= 32;
dfs(i + 1);
}
}
}
// Accepted solution for LeetCode #784: Letter Case Permutation
func letterCasePermutation(s string) (ans []string) {
t := []byte(s)
var dfs func(int)
dfs = func(i int) {
if i >= len(t) {
ans = append(ans, string(t))
return
}
dfs(i + 1)
if t[i] >= 'A' {
t[i] ^= 32
dfs(i + 1)
}
}
dfs(0)
return ans
}
# Accepted solution for LeetCode #784: Letter Case Permutation
class Solution:
def letterCasePermutation(self, s: str) -> List[str]:
def dfs(i: int) -> None:
if i >= len(t):
ans.append("".join(t))
return
dfs(i + 1)
if t[i].isalpha():
t[i] = chr(ord(t[i]) ^ 32)
dfs(i + 1)
t = list(s)
ans = []
dfs(0)
return ans
// Accepted solution for LeetCode #784: Letter Case Permutation
impl Solution {
pub fn letter_case_permutation(s: String) -> Vec<String> {
fn dfs(i: usize, t: &mut Vec<char>, ans: &mut Vec<String>) {
if i >= t.len() {
ans.push(t.iter().collect());
return;
}
dfs(i + 1, t, ans);
if t[i].is_alphabetic() {
t[i] = (t[i] as u8 ^ 32) as char;
dfs(i + 1, t, ans);
}
}
let mut t: Vec<char> = s.chars().collect();
let mut ans = Vec::new();
dfs(0, &mut t, &mut ans);
ans
}
}
// Accepted solution for LeetCode #784: Letter Case Permutation
function letterCasePermutation(s: string): string[] {
const t = s.split('');
const ans: string[] = [];
const dfs = (i: number) => {
if (i >= t.length) {
ans.push(t.join(''));
return;
}
dfs(i + 1);
if (t[i].charCodeAt(0) >= 65) {
t[i] = String.fromCharCode(t[i].charCodeAt(0) ^ 32);
dfs(i + 1);
}
};
dfs(0);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.