Letter Case Permutation
Generate all letter-case permutations of a string by branching into lowercase and uppercase variants for each alphabetic character.
01The problem
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.
| Input | Output | Why |
|---|---|---|
s = "a1b2" | ["a1b2", "a1B2", "A1b2", "A1B2"] | Both 'a' and 'b' can be uppercase or lowercase |
s = "3z4" | ["3z4", "3Z4"] | Only 'z' can change case |
Constraints: 1 <= s.length <= 12, s consists of lowercase English letters, uppercase English letters, and digits.
02How to spot it
- For each character in the string:
- If it's a digit: there is only 1 choice (keep as is).
- If it's a letter: there are 2 choices (lowercase or uppercase).
- We need to generate all combinations across all positions.
- With at most 12 characters,
2^12 = 4096combinations is very small.
So: binary decision tree per character.
03Pseudocode
build(i):
if i == n: record the string
elif s[i] is a letter:
s[i] = lower ; build(i + 1)
s[i] = upper ; build(i + 1)
else: build(i + 1)- If index
i == s.length, convert character array to string and record. - If character at
iis a letter: set to lowercase, recurse; set to uppercase, recurse. - If digit: recurse directly on
i + 1.
04Watch it work
working string chars
recorded words
0
Generate all letter-case combinations of "a1b2". Digits remain unchanged.
Pseudocode
- build(i):
- if i == n: record the string
- elif s[i] is a letter:
- s[i] = lower ; build(i + 1)
- s[i] = upper ; build(i + 1)
- else: build(i + 1)
Variables
- s
- a1b2
05Step-by-step dry run
- 1
working string chars
a011b223irecorded words
0
Generate all letter-case combinations of "a1b2". Digits remain unchanged.
s = a1b2
- 2
working string chars
a011b223irecorded words
0
Index 0 ('a'): branch 1 with lowercase 'a'. Recurse.
i = 0setChar = a
- 3
working string chars
a011b223irecorded words
0
Character '1' at index 1 is a digit. Only 1 branch: advance to index 2.
i = 1c = 1
- 4
working string chars
a011b223irecorded words
0
Index 2 ('b'): branch 1 with lowercase 'b'. Recurse.
i = 2setChar = b
- 5
working string chars
a011b223irecorded words
0
Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.
i = 3c = 2
- 6
working string chars
a011b223recorded words
1
Leaf reached: recorded "a1b2". Total: 1.
word = a1b2
- 7
working string chars
a011B223irecorded words
1
Index 2 ('b'): branch 2 with uppercase 'B'. Recurse.
i = 2setChar = B
- 8
working string chars
a011B223irecorded words
1
Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.
i = 3c = 2
- 9
working string chars
a011B223recorded words
2
Leaf reached: recorded "a1B2". Total: 2.
word = a1B2
- 10
working string chars
A011b223irecorded words
2
Index 0 ('a'): branch 2 with uppercase 'A'. Recurse.
i = 0setChar = A
- 11
working string chars
A011b223irecorded words
2
Character '1' at index 1 is a digit. Only 1 branch: advance to index 2.
i = 1c = 1
- 12
working string chars
A011b223irecorded words
2
Index 2 ('b'): branch 1 with lowercase 'b'. Recurse.
i = 2setChar = b
- 13
working string chars
A011b223irecorded words
2
Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.
i = 3c = 2
- 14
working string chars
A011b223recorded words
3
Leaf reached: recorded "A1b2". Total: 3.
word = A1b2
- 15
working string chars
A011B223irecorded words
3
Index 2 ('b'): branch 2 with uppercase 'B'. Recurse.
i = 2setChar = B
- 16
working string chars
A011B223irecorded words
3
Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.
i = 3c = 2
- 17
working string chars
A011B223recorded words
4
Leaf reached: recorded "A1B2". Total: 4.
word = A1B2
06Java solution
import java.util.ArrayList;
import java.util.List;
public class LetterCasePermutation {
public static List<String> letterCasePermutation(String s) {
List<String> res = new ArrayList<>();
build(s.toCharArray(), 0, res);
return res;
}
private static void build(char[] c, int i, List<String> res) {
if (i == c.length) {
res.add(new String(c));
return;
}
if (Character.isDigit(c[i])) {
build(c, i + 1, res);
} else {
c[i] = Character.toLowerCase(c[i]);
build(c, i + 1, res);
c[i] = Character.toUpperCase(c[i]);
build(c, i + 1, res);
}
}
public static void main(String[] args) {
System.out.println(letterCasePermutation("a1b2"));
System.out.println(letterCasePermutation("3z4"));
}
}[a1b2, a1B2, A1b2, A1B2] [3z4, 3Z4]
07Why it works
Mutating a single char[] array in place avoids creating intermediate substring objects.
When c[i] is a letter, the algorithm branches into both possibilities (toLowerCase and toUpperCase).
When i == c.length, new String(c) snapshots the current full string.
Because each position overwrites c[i] explicitly for both branches, no extra undo/restore step is necessary.
08Time and space
- Time complexity:
O(n * 2^L), whereLis the number of letters ins(L <= n). There are2^Lleaf states, each copyingncharacters. - Space complexity:
O(n)call stack depth forncharacters.
09Edge cases
- String of all digits (e.g.
"12345"): Only 1 branch, returns["12345"]. - String of all letters (e.g.
"ab"):2^2 = 4combinations. - Single character:
"a"->["a", "A"].
10Common mistakes
- Using string concatenation in recursion parameters. Writing
build(s + lower)createsO(n)garbage string objects at every tree node. Modifyingchar[]in place is faster and uses less memory.
11Other approaches
| Approach | Time | Space | Notes |
|---|---|---|---|
In-place char[] DFS | O(n * 2^L) | O(n) stack | Optimal and cleanest |
| BFS Queue | O(n * 2^L) | O(n * 2^L) | Expands queue level by level |
| Bitmasking | O(n * 2^L) | O(1) extra | Bit j controls case of j-th letter |
12In the interview
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why is modifying a single char[] in place superior to passing String substrings?
2. How many total outputs are generated for the string "123"?
14Solve it yourself
Try implementing the BFS queue solution:
- Initialize a queue with
"". - For each character, poll existing prefixes and append lowercase and uppercase variants.