Overlap Ratio of Two Token Lists
Arrays & HashingAuto-graded · 22 test casesLast updated
Compute the overlap ratio between the distinct strings of two lists using hash sets, with exact string matching and a defined result when both lists are empty.
Overlap Ratio of Two Token Lists
You are given two lists of strings, first and second. Measure how much their contents overlap by computing the ratio of shared distinct strings to all distinct strings that appear in either list.
Task
Write a function that returns this ratio as a float:
(number of distinct strings present in both lists) / (number of distinct strings present in at least one list)
Rules
- Only distinct strings matter. If a string appears several times in one list, it counts once for that list.
- Strings are compared exactly. Uppercase and lowercase letters are different, and spaces (leading, trailing, or inside) are part of the string. Do not trim or change the case of anything.
- The empty string
""is an ordinary string value. A list containing only""is not an empty list. - If both lists are empty, return
1.0. - If exactly one list is empty, return
0.0. - Do not use third-party libraries.
- Aim for time proportional to the total size of the two inputs.
Constraints
firstandsecondeach contain zero or more strings.- Every element is a Python
str, possibly empty. - The return value is a float.
Examples
Example 1
Input: overlap_ratio(['red', 'blue', 'blue'], ['blue', 'green'])
Output: 0.3333333333333333
Example 2
Input: overlap_ratio(['a', 'b'], ['a', 'b'])
Output: 1
Example 3
Input: overlap_ratio([], ['a'])
Output: 0
Example 4
Input: overlap_ratio([], [])
Output: 1
Hints
Hint 1
Collapse each list into a set first. That handles repeated strings and gives fast membership checks.
Hint 2
You do not need to build the union explicitly: its size equals |A| + |B| - |A ∩ B|.
Hint 3
Division by zero can only happen when both sets are empty, which the statement defines as 1.0. Check emptiness by size, not by whether elements are truthy, since [""] is non-empty.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Convert each list into a set. This removes duplicates and makes membership tests O(1) on average. Python's string equality and hashing are exact, so case and whitespace sensitivity come for free, and no normalization should be applied.
The only unsafe case is dividing by zero, which happens only when both sets are empty. The statement defines that result as 1.0, so we return it directly.
Otherwise:
- Count the strings of the smaller set that also appear in the larger set. This is the intersection size.
- Compute the union size with inclusion-exclusion:
|A| + |B| - |A ∩ B|. - Return
intersection / union.
Why it is correct
- Sets collapse repeated strings, so duplicates cannot affect the numerator or denominator.
- Each element of the smaller set is counted at most once, and it is counted exactly when it also belongs to the larger set, so the count is the true intersection size.
|A| + |B|counts shared strings twice, so subtracting the intersection once gives the union size.- If exactly one list is empty, the intersection is 0 and the union is positive, so the formula yields
0.0naturally. [""]is a non-empty set containing the empty string, so it is not confused with[], because emptiness is checked on the set rather than on element truthiness.- Using
/produces a float. Whole-number results such as1.0and0.0compare equal to the integers1and0in the sample outputs.
Complexity
- Time: O(n + m) expected, where n and m are the list lengths (ignoring the cost of hashing each string's characters).
- Space: O(n + m) for the two sets.
Reference solution
from typing import List
def overlap_ratio(first: List[str], second: List[str]) -> float:
set_a = set(first)
set_b = set(second)
# Both empty: the union is empty, so the result is defined as 1.0.
if not set_a and not set_b:
return 1.0
# Iterate over the smaller set when counting shared strings.
small, large = (set_a, set_b) if len(set_a) <= len(set_b) else (set_b, set_a)
shared = sum(1 for item in small if item in large)
total = len(set_a) + len(set_b) - shared
return shared / total