Generate every uppercase and lowercase variant of a string

Quick Overview

Given a string of letters and digits, return every string you can form by switching each letter between lowercase and uppercase, sorted by character code. The problem tests backtracking or iterative expansion, counting the outputs, and producing them in a deterministic order.

Generate every uppercase and lowercase variant of a string

Company: Yelp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string. Digits stay as they are. Return a list of all possible strings you could create. ### Function Signature ```python def letter_case_permutation(s: str) -> list[str]: ``` ### Rules - Return the strings sorted in ascending order by character code, which is Python's default string order. Digits sort before uppercase letters, and an uppercase letter sorts before its lowercase form (`'A' < 'a'`). - Each string appears once in the result. The original `s` is included as one of the strings. ### Constraints - `1 <= len(s) <= 12` - `s` consists of lowercase English letters, uppercase English letters, and digits. ### Examples ```text Input: s = "a1b2" Output: ["A1B2", "A1b2", "a1B2", "a1b2"] ``` Each of `a` and `b` can be lowercase or uppercase, giving 2 * 2 = 4 strings. In sorted order the uppercase choice comes first at each letter position. ```text Input: s = "3z4" Output: ["3Z4", "3z4"] ``` Only `z` is a letter, so there are two strings, and `"3Z4"` sorts before `"3z4"`. ```text Input: s = "12345" Output: ["12345"] ``` There are no letters, so the only string is `s` itself.

Overview: Given a string of letters and digits, return every string you can form by switching each letter between lowercase and uppercase, sorted by character code. The problem tests backtracking or iterative expansion, counting the outputs, and producing them in a deterministic order.

Yelp logo
Yelp
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string. Digits stay as they are.

Return a list of all possible strings you could create.

Function Signature

def letter_case_permutation(s: str) -> list[str]:

Rules

  • Return the strings sorted in ascending order by character code, which is Python's default string order. Digits sort before uppercase letters, and an uppercase letter sorts before its lowercase form ( 'A' < 'a' ).
  • Each string appears once in the result. The original s is included as one of the strings.

Constraints

  • 1 <= len(s) <= 12
  • s consists of lowercase English letters, uppercase English letters, and digits.

Examples

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

Each of a and b can be lowercase or uppercase, giving 2 * 2 = 4 strings. In sorted order the uppercase choice comes first at each letter position.

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

Only z is a letter, so there are two strings, and "3Z4" sorts before "3z4".

Input: s = "12345"
Output: ["12345"]

There are no letters, so the only string is s itself.