File: count-common-words-with-one-occurrence/solution.py

Date: 2026-06-06

Time: 15:55

count-common-words-with-one-occurrence/solution.py

Purpose

This file solves LeetCode 2085 — Count Common Words With One Occurrence. It owns a single responsibility: given two string arrays, count how many strings appear exactly once in both arrays. It follows the repo's convention of one Solution class per problem directory.

Key Components

Solution.countWords(words1, words2) -> int — The sole method. Takes two lists of strings and returns the count of words that have frequency 1 in words1 AND frequency 1 in words2.

Patterns

Dependencies

Flow

1. Build c1: frequency map of all words in words1.

2. Build c2: frequency map of all words in words2.

3. Iterate over keys in c1. For each word w, check if c1[w] == 1 (unique in first array) and c2[w] == 1 (unique in second array).

4. Sum the matches and return.

Time complexity: O(n + m) where n = len(words1), m = len(words2). Space: O(n + m) for the two counters.

Invariants

Error Handling

None. The method trusts its inputs match the LeetCode contract (non-empty lists of lowercase strings). No validation, no try/except — appropriate for a competitive-programming solution.

Topics to Explore

Beliefs