Date: 2026-06-06
Time: 19:18
This file solves LeetCode 1876: Substrings of Size Three with Distinct Characters. It counts how many length-3 substrings of a given string have all three characters distinct.
It follows the repo's convention: one Solution class per problem directory, with the solving method on that class.
Solution.highest_island(self, s: str) -> intNote the naming bug: the method is called highestisland, which is clearly a copy-paste artifact from a different problem. The LeetCode platform expects countGoodSubstrings, but within this repo the tests likely call highestisland directly, so it works despite the wrong name.
Contract: Given a string s of lowercase English letters, returns the count of substrings of length 3 where all three characters are different.
Sliding window (fixed size 3): The loop iterates i from 0 to len(s) - 3 (inclusive), examining the triple s[i], s[i+1], s[i+2] each iteration. Rather than using a set or len(set(...)) == 3, it manually checks all three pairwise inequalities. This is a micro-optimization — three comparisons vs. set construction — that's idiomatic for fixed-size windows small enough to enumerate pairs.
Accumulator pattern: A running count is incremented when the condition holds, then returned.
Imports: None. The solution is self-contained with no standard library or third-party imports.
Imported by: The "Imported By" list in the prompt is misleading — it lists hundreds of test files across unrelated problems. This is likely an artifact of a shared test harness or import-scanning tool that resolves solution.py generically. The real consumer is substrings-of-size-three-with-distinct-characters/test_solution.py.
1. Initialize count = 0.
2. For each index i in [0, len(s) - 3]:
s[i] != s[i+1], s[i+1] != s[i+2], and s[i] != s[i+2].count.3. Return count.
Time complexity: O(n). Space complexity: O(1).
range(len(s) - 2) guarantees i+2 is always a valid index, so no bounds checking is needed.len({s[i], s[i+1], s[i+2]}) == 3 — all three characters in the window must be distinct.range(len(s) - 2) produces an empty range, so the function correctly returns 0.None. The function assumes s is a valid string. An empty string or string shorter than 3 characters returns 0 naturally via the empty loop range. No exceptions are raised or caught.
substrings-of-size-three-with-distinct-characters/test_solution.py — See what test cases exercise this solution, including edge casessubstrings-of-size-three-with-distinct-characters/review.md — The code review may flag the highest_island naming issuemethod-naming-consistency — Several solutions in this repo may have copy-paste method names from other problems; worth auditingcount-vowel-substrings-of-a-string/solution.py:Solution — A harder variant of substring-with-distinct-characters using a variable-size sliding windowdiet-plan-performance/solution.py — Another fixed-size sliding window problem for comparisonwrong-method-name — The method is named highest_island instead of countGoodSubstrings; this is a copy-paste error from a different problempairwise-check-equivalence — The three pairwise != checks are logically equivalent to len(set(window)) == 3 for a window of size 3empty-input-safe — Strings with fewer than 3 characters return 0 without any special-case code, because range(len(s) - 2) is emptyconstant-space — The solution uses O(1) auxiliary space regardless of input size