2Brute2Best
🔥0

Letter Case Permutation

Medium·LeetCode #784 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get a string s made of letters and digits. Every letter can be changed to lower case or upper case on its own. Digits never change.

Return every string you can make this way. The answer can come back in any order.

Input

A string s of lowercase letters, uppercase letters and digits. It has between 1 and 12 characters.

Output

A list of every string you can make by choosing the case of each letter.

Example 1
Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]

There are two letters, a and b. Each one has two versions, so 2 × 2 = 4 strings. The digits 1 and 2 stay the same.

Example 2
Input: s = "3z4"
Output: ["3z4","3Z4"]

The only letter is z, which can be z or Z. The digits 3 and 4 never change.

Constraints
  • • 1 ≤ s.length ≤ 12
  • • s consists only of lowercase English letters, uppercase English letters and digits.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“transform every letter individually”
→ Each letter position makes its own decision, like Permutations: one decision per position.
“lowercase or uppercase”
→ Exactly two options per letter. This is take-one-of-two, not take-one-of-n.
“digits do not change”
→ A position with only one option makes no branch at all.
03

Approach Complexity Ladder

Brute Force — bitmask over the lettersO(2^L · N)O(N)

Loop over the 2^L masks; each bit picks the case of one letter; rebuild each string

Optimal — recurse over positions, branch only on lettersO(2^L · N)O(N)

Walk the string once; split only at letters into lower and upper; digits pass through

Brute Force Intuition

Count the L letters. Loop over every mask from 0 to 2^L − 1. Bit j of the mask decides the case of the j-th letter. For each mask, rebuild the whole string from scratch.

Recognition rule for this sub-pattern:

Pick an unused element for each position; mark used, recurse, unmark.
Remember this · Backtracking
Trigger
"All subsets / permutations / combinations", place items under rules (N-Queens, Sudoku), find a path in a grid.
Think
At each step, what are my choices, and how do I undo a choice?
Key question
What is my state, when is it complete, and when do I prune?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.