Skip to content

Commit bec4e98

Browse files
authored
Merge pull request #3189 from itsmohitnarayan/main
Created: 0350-Intersection-of-two-arrays-II.py
2 parents ac3f8e3 + a58f289 commit bec4e98

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Diff for: python/0079-word-search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def dfs(r, c, i):
2525
return res
2626

2727
# To prevent TLE,reverse the word if frequency of the first letter is more than the last letter's
28-
count = defaultdict(int, sum(map(Counter, board), Counter()))
28+
count = sum(map(Counter, board), Counter())
2929
if count[word[0]] > count[word[-1]]:
3030
word = word[::-1]
3131

Diff for: python/0350-intersection-of-two-arrays-ii.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
counter1 = Counter(nums1)
4+
counter2 = Counter(nums2)
5+
6+
# Using defaultdict to handle missing keys more efficiently
7+
counter1 = defaultdict(int, counter1)
8+
counter2 = defaultdict(int, counter2)
9+
10+
intersection = []
11+
12+
for num, freq in counter1.items():
13+
min_freq = min(freq, counter2[num])
14+
if min_freq > 0:
15+
intersection.extend([num] * min_freq)
16+
17+
return intersection

0 commit comments

Comments
 (0)