File: shortest-completing-word/solution.py

Date: 2026-06-06

Time: 19:04

Purpose

This file solves LeetCode 748: Shortest Completing Word. It owns both the solution and its test suite in a single module — the standard layout for this repo where each problem directory contains a solution.py with implementation + inline tests.

The problem: given a license plate string (mixed letters, digits, spaces) and a list of lowercase words, find the shortest word that contains every letter from the plate (ignoring digits/spaces, case-insensitive). Ties go to the word appearing first.

Key Components

shortestCompletingWord(licensePlate, words) -> str

The sole public function. Contract:

TestShortestCompletingWord

Seven test cases covering: basic examples, single-letter plates, tie-breaking by order, mixed-case plates, plates that are mostly digits/spaces, and repeated letters requiring exact multiplicity.

Patterns

Counter subtraction for multiset containment. The key idiom is on line 16:


if not (plate - Counter(word)):

Counter._sub_ drops zero/negative counts, so plate - Counter(word) yields an empty Counter (falsy) exactly when word contains at least as many of every letter as plate requires. This is a standard Python idiom for "is A a sub-multiset of B."

Linear scan with running minimum. Rather than sorting or using min() with a key, the code iterates once and tracks result manually. This preserves first-occurrence tie-breaking naturally — a later word of equal length won't replace result because of the strict <.

Dependencies

Imports: collections.Counter (multiset operations), unittest (test harness).

Imported by: The "Imported By" list in the prompt is misleading — those are *other* problems' test files that likely share a common test runner or import pattern, not files that actually import shortestCompletingWord. The only genuine consumer is shortest-completing-word/test_solution.py.

Flow

1. Extract plate letters: Generator expression filters licensePlate to alpha characters, lowercases them, feeds into Counter. This produces a frequency map like {'s': 1, 'p': 1, 't': 1} for "1s3 PSt".

2. Scan words: For each word, build its Counter and subtract the plate counter. If the result is empty (all plate letters are satisfied), check if this word is shorter than the current best.

3. Return: The first shortest completing word found, or None if none exists.

Time complexity: O(n·m) where n = number of words and m = average word length (for building each word's Counter). Space: O(1) extra beyond the counters (plate alphabet is bounded at 26).

Invariants

Error Handling

None. The function assumes valid inputs per LeetCode guarantees. If words is empty or no word completes the plate, it returns None — no exception raised. The Counter subtraction is safe on any string input.

Topics to Explore

Beliefs