File: maximum-number-of-words-you-can-type/solution.py

Date: 2026-06-06

Time: 17:40

Purpose

This file is the solution and test suite for LeetCode 1935 — Maximum Number of Words You Can Type. It owns the complete implementation: the algorithm in Solution.canBeTypedWords and the verification in TestSolution. Each problem directory in this repo follows the same structure (solution.py, test_solution.py, plan.md, review.md), though here the solution and tests are combined in a single file.

Key Components

Solution.canBeTypedWords(text, brokenLetters) -> int

Takes a space-separated string of words and a string of distinct broken letter keys. Returns how many words can be fully typed — meaning none of their characters appear in the broken set.

The implementation is two lines:

1. Convert brokenLetters to a set for O(1) membership checks.

2. Split text on spaces, test each word with set.isdisjoint(), and sum the boolean results.

TestSolution

Seven test cases covering: the three LeetCode examples, no broken letters, a single word that can't be typed, single-character words, and all letters broken.

Patterns

Dependencies

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

Imported by: The "Imported By" list in the prompt is misleading — it lists hundreds of test_solution.py files across unrelated problems. This is likely an artifact of the analysis tool matching on import unittest or the common Solution class name, not actual cross-problem imports. This module is self-contained and not imported by other solutions.

Flow

1. brokenLettersset(brokenLetters) — O(b) construction.

2. text.split() → list of words — O(n) split on whitespace.

3. For each word, broken.isdisjoint(word) — returns True if no character in word is in broken. Internally iterates over the shorter of the two sets/iterables.

4. sum(...) counts the True values → final count.

Invariants

Error Handling

None. The function trusts its inputs match the LeetCode contract. No defensive checks, no exceptions raised. This is appropriate — LeetCode guarantees valid inputs, and the set operations handle edge cases (empty brokenLetters → empty set → isdisjoint always returns True).