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.

Given a string `s`, you can change each letter on its own to lowercase or uppercase to create another string. Digits stay as they are. Return a list of all the strings you could create this way. Implement `letter_case_permutation(s)`, which takes the string `s` and returns a list of strings. ### 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. ### 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"`. ### Constraints - `1 <= len(s) <= 12` - `s` consists of lowercase English letters, uppercase English letters, and digits.

Constraints

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

Examples

Input: ('a',)

Expected Output: ['A', 'a']

Explanation: Minimum length with one lowercase letter: its uppercase form sorts first.

Input: ('Q',)

Expected Output: ['Q', 'q']

Explanation: Single uppercase letter: s itself comes first, then its lowercase form.

Hints

  1. Each letter position has two choices and each digit has one. How many strings does a given number of letters produce?
  2. Every result string has the same length as s, so two of them compare at the first position where they differ. At a letter position, which of the two characters comes first?
  3. Try an input whose letters are already uppercase: s must still appear, along with every lowercase choice.

All Letter-Case Variants of a String in Character-Code Order

mediumYelpSoftware Engineer

Given a string s, you can change each letter on its own to lowercase or uppercase to create another string. Digits stay as they are.

Return a list of all the strings you could create this way.

Implement letter_case_permutation(s), which takes the string s and returns a list of strings.

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.

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".

Constraints

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

Examples

Example 1
Input
('a',)
Output
['A', 'a']
Notes
Minimum length with one lowercase letter: its uppercase form sorts first.
Example 2
Input
('Q',)
Output
['Q', 'q']
Notes
Single uppercase letter: s itself comes first, then its lowercase form.

Constraints

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def letter_case_permutation(s) -> list[str]:
Sample call
letter_case_permutation('a')
Language
Case 1
Input
('a',)
Expected
['A', 'a']
Case 2
Input
('Q',)
Expected
['Q', 'q']

Browse More Questions

Show the approach

Approach

Build the answer one character at a time. Keep a list of every variant of the prefix read so far. When the next character is a digit, append it to each variant. When it is a letter, replace each variant p by two strings: p plus the uppercase letter, then p plus the lowercase letter.

Invariant: after reading i characters, the list holds every variant of the first i characters of s exactly once, each of length i, in ascending character-code order. It starts true with the list [""]. Appending a digit to every entry keeps the order. For a letter, take two entries p < q: they have the same length, so they differ at some earlier position, and every extension of p sorts before every extension of q. The two extensions of the same p differ only in the last character, and an uppercase letter (codes 65 to 90) sorts before a lowercase letter (codes 97 to 122). So the new list is sorted, and since each variant comes from exactly one sequence of case choices, no string repeats. No final sort is needed.

Edge cases: a string with no letters returns [s]. Letters that are already uppercase in s still produce both forms, and s itself always appears in the result. With k letters the result has 2^k strings, at most 2^12 = 4096, so memory stays small in every language and no integer value is involved.

Time complexity:
O(n * 2^k), where n = len(s) and k is the number of letters in s (at most O(n * 2^n))
Space complexity:
O(n * 2^k) for the output list