# Easy 2 lines O(N^2) Python

• ``````def fourSumCount(self, A, B, C, D):
AB = collections.Counter(a+b for a in A for b in B)
return sum(AB[-c-d] for c in C for d in D)``````

• This post is deleted!

• @slcott You're using the same library I'm using.

• This post is deleted!

• @slcott `collections` isn't a library. It's a module. And it's part of the Python standard library, just like the dict you're using.

• This post is deleted!

• every time I can learn a lot from your code, thanks for sharing!

• Brilliant answer, thanks a lot for sharing!

• Thank you for the code. It is really a brilliant and concise solution.

• brilliant answer, thank U!

• @StefanPochmann Brilliant man! Beautiful and elegant solution. Power of python.

• Wow beautiful

• Amazing...Amazing...Amazing...

• You are an amazing human being

• @StefanPochmann How does the code get us the sum of 0? Can you give some explanation?

• ``````  # O(n**2) time, O(n**2) space
def fourSumCount(self, A, B, C, D):
count = 0
num_dict = {}
for i in C:
for j in D:
s = i + j
if s in num_dict:
num_dict[s] += 1
else:
num_dict[s] = 1

for i in range(len(A)):
for j in range(len(B)):
target = 0 - (A[i]+B[j])
if target in num_dict:
count += num_dict[target]
return count
``````

• This solution is amazing concise ! Learn a lot from @StefanPochmann

• @Lakeriver Please check https://docs.python.org/2/library/collections.html, -c-d is calculated and then check if it is in the colletions.Counter()

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.