Magicsheet logo

Letter Case Permutation

Medium
97.5%
Updated 6/1/2025

Letter Case Permutation

What is this problem about?

The "Letter Case Permutation interview question" asks you to generate all possible strings by changing the case of each letter in a given string. Numbers remain unchanged. For example, "a1b2" can become "a1b2", "A1b2", "a1B2", or "A1B2". This "Letter Case Permutation coding problem" is a classic combinatorial problem that explores subsets and branching logic.

Why is this asked in interviews?

This is a standard question to test a candidate's understanding of the "Backtracking interview pattern" or recursion. Companies like Apple and Microsoft use it to see if a candidate can visualize a decision tree and implement a solution that visits all nodes. It also tests basic character manipulation (checking if a character is a letter and toggling its case).

Algorithmic pattern used

The primary pattern is Backtracking or DFS. You start from the first character. If it's a digit, you simply move to the next. If it's a letter, you have two choices: keep it as it is or change its case. You recursively call the function for both choices. Once you reach the end of the string, you add the current configuration to your results.

Example explanation

Input: "3z"

  1. Index 0 ('3'): Digit, no choice. Move to index 1.
  2. Index 1 ('z'): Letter. Choice 1: "3z". Choice 2: "3Z".
  3. End: Add "3z" and "3Z" to the result list. Result: ["3z", "3Z"]

Common mistakes candidates make

  • Redundant Toggling: Toggling the case multiple times instead of just passing the modified character to the next recursive call.
  • Not handling digits: Forgetting that digits should just be skipped without branching.
  • String immutability: Not handling string concatenation efficiently (using a character array is often better in languages like Java or C++).

Interview preparation tip

Practice visualizing recursion as a tree. For this problem, each letter is a branch point with two children, and each digit is a path with only one child. This mental model helps in understanding both the logic and the time complexity (O(2^n)).

Similar Questions