Skip to content
LC-2085 Easy LeetCode

2085. Count Common Words With One Occurrence

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 72% Topics: Array, Hash Table, String, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(m + n)
# Space: O(m + n)

import collections


class Solution(object):
    def countWords(self, words1, words2):
        """
        :type words1: List[str]
        :type words2: List[str]
        :rtype: int
        """
        cnt = collections.Counter(words1)
        for c in words2:
            if cnt[c] < 2:
                cnt[c] -= 1
        return sum(v == 0 for v in cnt.itervalues())

Solution from kamyu104/LeetCode-Solutions · MIT