File: check-if-string-is-decomposable-into-value-equal-substrings/solution.py

Date: 2026-06-06

Time: 15:43

check-if-string-is-decomposable-into-value-equal-substrings/solution.py

Purpose

This file solves LeetCode 1933 — given a digit string, determine whether it can be partitioned into consecutive substrings of identical characters where every group has length 3, except exactly one group which has length 2.

Key Components

isdecomposableintovalueequal_substrings(s: str) -> bool — The sole function. It takes a digit string and returns whether a valid decomposition exists. The contract: every maximal run of identical characters must be fully consumed by some mix of length-3 and length-2 chunks, with exactly one length-2 chunk across the entire string.

Patterns

The solution uses run-length encoding via itertools.groupby — a common idiom for LeetCode string problems involving consecutive identical characters. Rather than materializing groups, it counts each group's length with sum(1 for _ in group) (consuming the iterator without allocation).

The key insight is modular arithmetic: for a run of length n consisting of one repeated character, the only way to tile it with 3s and 2s is to use n // 3 threes and at most one two. So:

Wait — actually n % 3 == 1 with n >= 4 could be tiled as, say, 4 = 2 + 2. But the problem says exactly one length-2 substring total. So a remainder of 1 from a single group would require two 2-chunks from that group (e.g., length 4 = 2+2), which would already exceed the "exactly one" constraint. And length 1 obviously can't be decomposed at all. So returning False for remainder 1 is correct — it's a short-circuit that catches both the impossible case (length 1) and the case that would overcount (using two 2-chunks from one group).

Flow

1. Initialize twos = 0 counter

2. Iterate over consecutive character groups via groupby(s)

3. For each group, compute remainder = len(group) % 3

4. If remainder == 1: return False immediately (no valid decomposition for this group)

5. If remainder == 2: increment twos

6. After all groups: return twos == 1 (exactly one length-2 substring total)

Dependencies

Imports: itertools.groupby — used for run-length grouping.

Imported by: check-if-string-is-decomposable-into-value-equal-substrings/test_solution.py directly. The massive "Imported By" list in the prompt appears to be an artifact of the repository's test infrastructure (likely a shared conftest or test runner), not actual imports of this function.

Invariants

Error Handling

None — pure function with no exceptions. Invalid inputs (non-string, None) would raise standard Python TypeError from groupby. The function trusts the caller to pass a valid digit string per the LeetCode contract.

Topics to Explore

Beliefs