-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountWords.py
More file actions
34 lines (27 loc) · 1.13 KB
/
countWords.py
File metadata and controls
34 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Given two string arrays words1 and words2, return the number of strings that appear exactly once in each of the
two arrays.
Example 1:
Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
Output: 2
Explanation:
- "leetcode" appears exactly once in each of the two arrays. We count this string.
- "amazing" appears exactly once in each of the two arrays. We count this string.
- "is" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.
- "as" appears once in words1, but does not appear in words2. We do not count this string.
Thus, there are 2 strings that appear exactly once in each of the two arrays.
"""
from collections import Counter
class Solution:
def countWords(self, words1: list[str], words2: list[str]) -> int:
counter1 = Counter(words1)
counter2 = Counter(words2)
res = 0
for word in counter1:
if counter1[word] > 1:
continue
if word not in counter2:
continue
if counter2[word] == 1:
res += 1
return res