4Sum II LeetCode solution in Python
1# Runtime: 1360 ms (Top 29.95%) | Memory: 14.2 MB (Top 70.39%)2class Solution:3def fourSumCount(4self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]5) -> int:6n1n2 = defaultdict(int)7for n1 in nums1:8for n2 in nums2:9n1n2[n1 + n2] += 110n3n4 = defaultdict(int)11for n3 in nums3:12for n4 in nums4:13n3n4[n3 + n4] += 114ans = 015for s in n1n2:16ans += n1n2[s] * n3n4[-s]17return ans