File: check-whether-two-strings-are-almost-equivalent/solution.py

Date: 2026-06-06

Time: 15:45

Purpose

This file implements LeetCode 2068: Check Whether Two Strings are Almost Equivalent. It belongs to the leetcode-implementations repo, where each problem lives in its own directory with a solution, tests, plan, and review.

The solution determines whether two strings are "almost equivalent" — meaning for every letter in the alphabet, the difference in frequency between the two strings is at most 3.

Key Components

Solution.checkAlmostEquivalent(self, word1, word2) -> bool — The single method, following LeetCode's expected class/method signature. It:

Patterns

Dependencies

Imports: collections.Counter — the only external dependency.

Imported by: The test file check-whether-two-strings-are-almost-equivalent/test_solution.py. The large "Imported By" list in the prompt is misleading — those are unrelated test files that happen to import from their own solution.py modules, not from this one.

Flow

1. Build freq1 from word1 and freq2 from word2 using Counter.

2. Compute the union of all characters present in either counter via set(freq1) | set(freq2).

3. For each character in that union, check if abs(freq1[c] - freq2[c]) > 3.

4. If any character exceeds the threshold, return False immediately.

5. If the loop completes, return True.

Invariants

Error Handling

None. The function assumes valid input per LeetCode constraints (lowercase English letters, non-empty strings). No exceptions are raised or caught.

Topics to Explore

Beliefs