SDE & MLE Prep

784. Letter Case Permutation

Problem

Change the case of any of the letters in s (digits stay as they are) and return every string you can get, in any order.

Example: "a1b2" → ["a1b2","a1B2","A1b2","A1B2"]

Constraints: length ≤ 12.

Each letter branches into lower/upper; digits don't branch. Building iteratively: for each letter, double the result list.

Complexity: O(2^L · n).

class Solution:
    def letterCasePermutation(self, s: str) -> List[str]:
        res = [""]
        for ch in s:
            if ch.isalpha():
                res = [p + c for p in res for c in (ch.lower(), ch.upper())]
            else:
                res = [p + ch for p in res]
        return res