File: reformat-phone-number/solution.py

Date: 2026-06-06

Time: 18:42

Purpose

This file solves LeetCode 1694: Reformat Phone Number. It takes a phone number string containing digits, spaces, and dashes, strips the non-digit characters, and re-groups the digits into blocks of 3 or 2 separated by dashes — following the specific grouping rules from the problem.

Key Components

Solution.reformatNumber

The only meaningful method. Contract:

min_subarray (module-level alias)

A quirk of the project's test template — every solution module exports a min_subarray alias regardless of the actual problem. This is why the "Imported By" list is enormous and includes hundreds of unrelated test files; they all share the same import pattern.

Flow

1. Strip non-digits: digits = ''.join(c for c in number if c.isdigit()) — filters to a pure digit string.

2. Greedy chunking by 3: The while len(digits) - i > 4 loop consumes digits in blocks of 3 as long as more than 4 digits remain. The > 4 threshold (not > 3) is the key insight — it prevents leaving exactly 4 digits to be handled as a single block (which would violate the "no block of size 4" rule).

3. Handle the tail (1–4 remaining digits):

4. Join with dashes: '-'.join(blocks).

Patterns

Dependencies

Invariants

Error Handling

None. The function assumes valid input per LeetCode constraints. An empty string input would produce an empty output without error; a string with no digits would produce "".

Topics to Explore

Beliefs