All problems
0712MediumStringBacktrackingBit Manipulation

Every Spelling of a Part Code

Tracked in this browser only
Write code

Trains the technique from

LeetCode 784Letter Case Permutation

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A warehouse scanner reads part codes made of English letters and digits, and it ignores letter case, so the same part code can be keyed in more than one way.

Return every spelling of code that can be produced by choosing, independently for each letter, whether that letter is written in uppercase or in lowercase. Digits are copied over unchanged.

The spellings may be returned in any order, and each distinct spelling must appear exactly once.

Examples

Example 1

Input
code = "p4q"
Output
["p4q", "p4Q", "P4q", "P4Q"]

Two letters each have two cases, so four spellings come out, and the digit 4 stays put in every one of them.

Example 2

Input
code = "58"
Output
["58"]

There is no letter to choose a case for, so the code has exactly one spelling, itself.

Example 3

Input
code = "Wz"
Output
["wz", "wZ", "Wz", "WZ"]

Each of the two letters is written either way, whatever case it arrived in, giving four spellings.

Constraints

  • 1 <= code.length <= 12
  • code consists of lowercase English letters, uppercase English letters and digits.

The values you return may be in any order.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def code_spellings(code: str) -> list[str]:
Java
public List<String> codeSpellings(String code)
September 7
Apply