Ctrl K

Words Listed Once in Both

Easy
Two people wrote shopping lists, first and second. Words may repeat within a list. Count how many distinct words appear **exactly once** in first and also **exactly once** in second.
Example 1
first=["milk", "bread", "milk", "eggs"], second=["eggs", "bread", "tea"] → 2

"bread" and "eggs" appear exactly once in each list. "milk" is missing from the second list (and appears twice in the first), "tea" is missing from the first.

Example 2
first=["apple"], second=["apple", "apple"] → 0

"apple" appears twice in the second list, so it does not count.

Example 3
first=["x", "y"], second=["z"] → 0
Constraints
  • 1 ≤ len(first), len(second) ≤ 10⁵
  • 1 ≤ length of each word ≤ 30
  • Words consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords From Letter Tiles EasyWords From Working Keys EasyThe k-th Word With No Repeats EasyPairs With the Same Letter Set EasyMost Searched Words Medium
loading Python…
Input
first=["milk", "bread", "milk", "eggs"], second=["eggs", "bread", "tea"]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.