4Sum II

 

454. 4Sum II

Solution

  1. Get 2Sum first and then combine the 2 2Sums.

  2. We only need to get one 2Sum, and get its negation from the other 2 arrays.

def fourSumCount(self, A, B, C, D):
    """
    :type A: List[int]
    :type B: List[int]
    :type C: List[int]
    :type D: List[int]
    :rtype: int
    """
    dic = collections.Counter(a + b for a in A for b in B)
    
    res = sum(dic[-c-d] for c in C for d in D)
    return res