Letter Case Permutation - 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.

Note: Only alphabetic characters can be transformed - digits and special characters remain unchanged.

Input & Output

Example 1 — Basic Case
$ Input: s = "a1b2"
Output: ["a1b2","a1B2","A1b2","A1B2"]
💡 Note: Two letters 'a' and 'b' can each be upper or lower case. 2×2 = 4 combinations total.
Example 2 — Single Character
$ Input: s = "3z4"
Output: ["3z4","3Z4"]
💡 Note: Only one letter 'z', so 2 possibilities: lowercase 'z' or uppercase 'Z'.
Example 3 — No Letters
$ Input: s = "12345"
Output: ["12345"]
💡 Note: No letters to transform, so only one result: the original string.

Constraints

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

Visualization

Tap to expand
Letter Case Permutation INPUT String s = "a1b2" a letter 1 digit b letter 2 digit = Can transform = Fixed (digit) Each letter has 2 choices: a --> a a --> A 2 letters = 2^2 = 4 results Digits stay unchanged ALGORITHM STEPS 1 Initialize Start with empty result [""] 2 Iterate Characters Process each char in string 3 Branch on Letters If letter: add both cases 4 Keep Digits If digit: append as-is Backtracking Tree: "" a A a1 A1 a1b a1B A1b A1B FINAL RESULT All 4 permutations generated: "a1b2" "a1B2" "A1b2" "A1B2" Output Array: ["a1b2","a1B2","A1b2","A1B2"] OK - Complete! Key Insight: Use backtracking to explore all possible case combinations. For each letter, branch into two paths (lowercase and uppercase). Digits have only one path. Time: O(2^n * n), Space: O(2^n * n) where n is string length. The result size doubles for each letter encountered. TutorialsPoint - Letter Case Permutation | Optimal Solution (Backtracking)
Asked in
Google 15 Facebook 12 Amazon 8
185.0K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen