File: substrings-of-size-three-with-distinct-characters/solution.py

Date: 2026-06-06

Time: 19:18

Purpose

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.

Key Components

Solution.highest_island(self, s: str) -> int

Note 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.

Patterns

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.

Dependencies

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.

Flow

1. Initialize count = 0.

2. For each index i in [0, len(s) - 3]:

3. Return count.

Time complexity: O(n). Space complexity: O(1).

Invariants

Error Handling

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.

Topics to Explore

Beliefs