Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #784: Letter Case Permutation

In this guide, we solve Leetcode #784 Letter Case Permutation in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Bit Manipulation, String, Backtracking

Intuition

We must explore combinations of choices, but many branches can be pruned early.

Backtracking enumerates valid candidates while keeping the search space under control.

Approach

Use DFS to build candidates step by step, and backtrack when constraints are violated.

Pruning keeps the exploration practical for typical constraints.

Steps:

  • Define the decision tree.
  • DFS through choices and backtrack.
  • Prune invalid paths early.

Example

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

Python Solution

class Solution: def letterCasePermutation(self, s: str) -> List[str]: def dfs(i: int) -> None: if i >= len(t): ans.append("".join(t)) return dfs(i + 1) if t[i].isalpha(): t[i] = chr(ord(t[i]) ^ 32) dfs(i + 1) t = list(s) ans = [] dfs(0) return ans

Complexity

The time complexity is O(n×2n)O(n \times 2^n)O(n×2n), where nnn is the length of the string sss. The space complexity is O(depth).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy