-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_common_words_with_one_occurrence.py
94 lines (74 loc) · 3.23 KB
/
count_common_words_with_one_occurrence.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
# Count Common Words With One Occurrence
#
# https://leetcode.com/problems/count-common-words-with-one-occurrence/
#
# Given two string arrays words1 and words2, return the number of strings that
# appear exactly once in each of the two arrays.
from typing import List
def test():
"""
Run `pytest <this-file>`.
"""
def test_algo(algo):
# indices in words1: [0, 5]
assert algo(["once both", "once here", "twice", "twice", "three", "1"], ["1", "another here", "three", "once both", "three"]) == 2
# Test all different algorithms/implementations
solution = Solution()
for algo in [solution.hashmap, solution.hashmap_cleaner]:
test_algo(algo)
class Solution:
def hashmap(self, words1: List[str], words2: List[str]) -> int:
"""
Approach: Store occurences in hashmap.
Idea: Maintain a hashmap mapping strings to occurring indices to quickly be able to lookup each string in both lists.
Time: O(n): Building up the hashmaps takes O(n), and the final iteration aggregating the indices takes O(n).
Space: O(n): The hashmap take O(n) space.
Leetcode: 69 ms runtime, 17.10 MB memory
"""
def appearing_indices(l: List[str]) -> dict[str, List[int]]:
"""Map each string to the indices in the list it appears at."""
map = dict()
for (i, e) in enumerate(l):
if not e in map:
map[e] = [i]
else:
map[e].append(i)
return map
freq1 = appearing_indices(words1)
freq2 = appearing_indices(words2)
indices = []
for (s, indices1) in freq1.items():
indices2 = freq2[s] if s in freq2 else []
if len(indices1) == len(indices2) == 1:
indices.append(indices1[0])
return len(indices)
def hashmap_cleaner(self, words1: List[str], words2: List[str]) -> int:
"""
Approach: Store only strings with single occurence in hashmap.
Idea: Maintain a hashmap mapping strings to their single occurring index to quickly be able to lookup each string in both lists.
Time: O(n): Building up the hashmaps takes O(n), and the final iteration aggregating the indices takes O(n).
Space: O(n): The hashmap take O(n) space.
Leetcode: 68 ms runtime, 17.20 MB memory
"""
def appearing_indices(l: List[str]) -> dict[str, int]:
"""Map each string that only appears once in the string to the index it appears at."""
map = dict()
for (i, s) in enumerate(l):
if not s in map:
map[s] = [i]
else:
map[s].append(i)
# Only keep strings with exactly one occurence.
reduced = dict()
for (s, indices) in map.items():
if len(indices) == 1:
reduced[s] = indices[0]
return reduced
freq1 = appearing_indices(words1)
freq2 = appearing_indices(words2)
indices = []
for (s, index1) in freq1.items():
if s in freq2:
indices.append(index1)
return len(indices)