File: count-pairs-of-similar-strings/solution.py

Date: 2026-06-06

Time: 16:02

Purpose

This file solves LeetCode 2506: Count Pairs of Similar Strings. Two strings are "similar" if they contain exactly the same set of distinct characters (ignoring frequency). The function counts all such pairs (i, j) where i < j.

Key Components

countsimilarpairs(words: list[str]) -> int

The sole public function. Takes a list of lowercase strings, returns the count of pairs sharing the same character set.

Contract: Input is a list of lowercase English strings. Output is a non-negative integer. No mutation of the input.

Patterns

Counting-based combinatorics. Instead of comparing every pair (O(n^2) comparisons), the solution groups words by their character fingerprint using Counter(frozenset(w) for w in words). Each group of size n contributes n*(n-1)//2 pairs — the standard "n choose 2" formula.

frozenset as a hashable signature. Converting each word to frozenset collapses character frequency down to character presence, which is exactly the "similar" predicate. frozenset is hashable (unlike set), so it works as a Counter key.

Dependencies

Imports: collections.Counter — used to tally how many words share each character set.

Imported by: The "Imported By" list in the prompt is misleading — those are test files across the entire repo that import Counter from collections, not files that import this module. The actual consumer is count-pairs-of-similar-strings/test_solution.py.

Flow

1. For each word w, compute frozenset(w) — the set of distinct characters.

2. Counter tallies how many words map to each distinct frozenset.

3. For each group of size n, accumulate n*(n-1)//2 (the number of unordered pairs within that group).

4. Return the sum.

Time complexity: O(n * k) where n is the number of words and k is average word length (for building frozensets). The final summation is O(u) where u is the number of unique character sets — at most min(n, 2^26).

Space complexity: O(u) for the Counter.

Invariants

Error Handling

None. The function trusts its caller to provide valid input (list of strings). An empty list returns 0 naturally since Counter produces no entries and sum of an empty generator is 0.

Topics to Explore

Beliefs