Date: 2026-06-06
Time: 18:42
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.
Solution.reformatNumberThe only meaningful method. Contract:
number containing at least 2 digits, plus optional spaces and dashes.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.
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).
i advances by 3 each iteration while the string stays immutable.str.isdigit, str.join).reformat-phone-number/testsolution.py is the only meaningful consumer. The hundreds of other test files in the "Imported By" list are an artifact of the shared minsubarray alias — they import from their own solution.py, not this one.> 4 threshold in the loop guard and the two-branch tail handling jointly enforce this.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 "".
reformat-phone-number/test_solution.py — See what edge cases the tests cover (4-digit tail, minimal input, mixed whitespace/dashes)reformat-phone-number/plan.md — The planning doc may explain why > 4 was chosen over alternative loop conditionsdivide-a-string-into-groups-of-size-k/solution.py:Solution — A related grouping problem that uses a fill character instead of variable block sizesgreedy-partitioning-pattern — How the greedy-then-fixup approach appears across other string/array grouping problems in this reporeformat-no-block-of-one — The loop guard len(digits) - i > 4 ensures the tail is never a single digit, so no output block has size 1reformat-tail-split-at-four — When exactly 4 digits remain, they are split into two blocks of 2 (not 3+1 or a single 4)min-subarray-alias-is-generic — The min_subarray alias at module level is a project-wide test harness convention, not semantically related to the solution