File: replace-all-digits-with-characters/solution.py

Date: 2026-06-06

Time: 18:51

Purpose

This file is the solution and test suite for LeetCode 1844: Replace All Digits with Characters. It owns the complete implementation — algorithm, module-level export alias, and unit tests — all in a single file, following the repo's convention of self-contained problem directories.

Key Components

Solution.replaceDigits(self, s: str) -> str

The core algorithm. Given a string where even-indexed positions hold lowercase letters and odd-indexed positions hold digit characters, it replaces each digit with the character obtained by shifting the preceding letter forward by that digit's value in the alphabet.

Contract: s must alternate between letters (even indices) and digits (odd indices). The method does not validate this — it trusts the LeetCode guarantee.

count_balls (module-level alias)


count_balls = Solution().replaceDigits

This is a misnamed export. The name count_balls belongs to a different LeetCode problem (1742: Maximum Number of Balls in a Box). It exists because this repo uses a uniform import convention across test files — the "Imported By" list shows hundreds of test files importing from this module, which means the test runner or generated test harness imports a canonical symbol name from each solution module. The name mismatch is a code-gen artifact, not a logic error.

TestReplaceDigits

Six test cases covering the standard examples, edge cases (single character, zero shift), and boundary behavior (shifting from 'z', large shift of 9).

Patterns

Dependencies

Imports: Only unittest from the standard library — no external dependencies.

Imported by: The massive "Imported By" list (300+ test files) is misleading. These files don't depend on the *logic* of this module — they import the count_balls alias as part of a generated test harness pattern. The dependency is structural (naming convention), not functional.

Flow

1. Convert input string s to a mutable list of characters.

2. Iterate over odd indices (1, 3, 5, …).

3. For each odd index i, read the preceding character at i-1, read the digit at i, compute chr(ord(prev_char) + digit), and write the result back to chars[i].

4. Join and return.

For input "a1c1e1":

Invariants

Error Handling

None. The code assumes valid input per the LeetCode contract. Passing a string with letters at odd indices would produce int() ValueError; passing an empty string works fine (the loop body never executes).

Topics to Explore

Beliefs