Skip to content
Veltoryx Technologies
FreeProblem 76● MediumLeetCode #784 ↗3 min readUpdated 30 October 2026

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.

InputOutputWhy
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 = 4096 combinations is very small.

So: binary decision tree per character.

03Pseudocode

text
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)
  1. If index i == s.length, convert character array to string and record.
  2. If character at i is a letter: set to lowercase, recurse; set to uppercase, recurse.
  3. If digit: recurse directly on i + 1.

04Watch it work

Visualizer · step through the algorithms = "a1b2"

working string chars

a0
11
b2
23
i

recorded words

0

Generate all letter-case combinations of "a1b2". Digits remain unchanged.

Pseudocode

  1. build(i):
  2. if i == n: record the string
  3. elif s[i] is a letter:
  4. s[i] = lower ; build(i + 1)
  5. s[i] = upper ; build(i + 1)
  6. else: build(i + 1)

Variables

s
a1b2
step 1 / 17

05Step-by-step dry run

  1. 1

    working string chars

    a0
    11
    b2
    23
    i

    recorded words

    0

    Generate all letter-case combinations of "a1b2". Digits remain unchanged.

    s = a1b2

  2. 2

    working string chars

    a0
    11
    b2
    23
    i

    recorded words

    0

    Index 0 ('a'): branch 1 with lowercase 'a'. Recurse.

    i = 0setChar = a

  3. 3

    working string chars

    a0
    11
    b2
    23
    i

    recorded words

    0

    Character '1' at index 1 is a digit. Only 1 branch: advance to index 2.

    i = 1c = 1

  4. 4

    working string chars

    a0
    11
    b2
    23
    i

    recorded words

    0

    Index 2 ('b'): branch 1 with lowercase 'b'. Recurse.

    i = 2setChar = b

  5. 5

    working string chars

    a0
    11
    b2
    23
    i

    recorded words

    0

    Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.

    i = 3c = 2

  6. 6

    working string chars

    a0
    11
    b2
    23

    recorded words

    1

    Leaf reached: recorded "a1b2". Total: 1.

    word = a1b2

  7. 7

    working string chars

    a0
    11
    B2
    23
    i

    recorded words

    1

    Index 2 ('b'): branch 2 with uppercase 'B'. Recurse.

    i = 2setChar = B

  8. 8

    working string chars

    a0
    11
    B2
    23
    i

    recorded words

    1

    Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.

    i = 3c = 2

  9. 9

    working string chars

    a0
    11
    B2
    23

    recorded words

    2

    Leaf reached: recorded "a1B2". Total: 2.

    word = a1B2

  10. 10

    working string chars

    A0
    11
    b2
    23
    i

    recorded words

    2

    Index 0 ('a'): branch 2 with uppercase 'A'. Recurse.

    i = 0setChar = A

  11. 11

    working string chars

    A0
    11
    b2
    23
    i

    recorded words

    2

    Character '1' at index 1 is a digit. Only 1 branch: advance to index 2.

    i = 1c = 1

  12. 12

    working string chars

    A0
    11
    b2
    23
    i

    recorded words

    2

    Index 2 ('b'): branch 1 with lowercase 'b'. Recurse.

    i = 2setChar = b

  13. 13

    working string chars

    A0
    11
    b2
    23
    i

    recorded words

    2

    Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.

    i = 3c = 2

  14. 14

    working string chars

    A0
    11
    b2
    23

    recorded words

    3

    Leaf reached: recorded "A1b2". Total: 3.

    word = A1b2

  15. 15

    working string chars

    A0
    11
    B2
    23
    i

    recorded words

    3

    Index 2 ('b'): branch 2 with uppercase 'B'. Recurse.

    i = 2setChar = B

  16. 16

    working string chars

    A0
    11
    B2
    23
    i

    recorded words

    3

    Character '2' at index 3 is a digit. Only 1 branch: advance to index 4.

    i = 3c = 2

  17. 17

    working string chars

    A0
    11
    B2
    23

    recorded words

    4

    Leaf reached: recorded "A1B2". Total: 4.

    word = A1B2

06Java solution

LetterCasePermutation.java
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"));
    }
}
Expected output
[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), where L is the number of letters in s (L <= n). There are 2^L leaf states, each copying n characters.
  • Space complexity: O(n) call stack depth for n characters.

09Edge cases

  • String of all digits (e.g. "12345"): Only 1 branch, returns ["12345"].
  • String of all letters (e.g. "ab"): 2^2 = 4 combinations.
  • Single character: "a" -> ["a", "A"].

10Common mistakes

  • Using string concatenation in recursion parameters. Writing build(s + lower) creates O(n) garbage string objects at every tree node. Modifying char[] in place is faster and uses less memory.

11Other approaches

ApproachTimeSpaceNotes
In-place char[] DFSO(n * 2^L)O(n) stackOptimal and cleanest
BFS QueueO(n * 2^L)O(n * 2^L)Expands queue level by level
BitmaskingO(n * 2^L)O(1) extraBit j controls case of j-th letter

12In the interview

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why is modifying a single char[] in place superior to passing String substrings?

  2. 2. How many total outputs are generated for the string "123"?

14Solve it yourself

Try implementing the BFS queue solution:

  1. Initialize a queue with "".
  2. For each character, poll existing prefixes and append lowercase and uppercase variants.