File: percentage-of-letter-in-string/solution.py

Date: 2026-06-06

Time: 18:31

percentage-of-letter-in-string/solution.py

Purpose

Solves LeetCode 2278 — given a string s and a character letter, return the floor percentage of characters in s that equal letter. This is a single-responsibility module: one class, one method, one expression.

Key Components

Solution.percentageLetter(self, s: str, letter: str) -> int — the only method. It computes count * 100 // len(s) in a single expression, returning an integer in [0, 100].

The method uses str.count() for occurrence counting and integer floor division (//) to satisfy the "rounded down" requirement. The multiplication by 100 happens *before* the division — this is critical. count * 100 // len(s) avoids floating-point entirely, which prevents rounding artifacts that int(count / len(s) * 100) could introduce.

Patterns

Dependencies

Imports: None. Pure stdlib — only uses str.count() and integer arithmetic.

Imported by: percentage-of-letter-in-string/test_solution.py directly. The massive "Imported By" list in the prompt is misleading — those are *other* problems' test files importing *their own* solution.py, not this one. The only real consumer is this problem's own test file.

Flow

1. s.count(letter) — O(n) scan counting occurrences of letter in s

2. Multiply by 100

3. Integer-divide by len(s)

4. Return the result

No loops, no branches, no mutation. The entire method is a single return statement.

Invariants

Error Handling

None. The method will raise ZeroDivisionError on empty string input, but the problem constraints prevent this. This is consistent with the repo's approach: solutions trust LeetCode's input guarantees rather than adding defensive checks.

Topics to Explore

Beliefs