LeetCode solutions

2766. Find the Prefix Common Array of Two Arrays

My accepted Python solution to LeetCode problem 2766, Find the Prefix Common Array of Two Arrays, running in 7ms.

  • Difficulty: Medium
  • Python
  • Runtime 7ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 7ms, memory 17.3MB, accepted 2026-01-01.

python
class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        n = len(A)
        result = []
        seen_a = set()
        seen_b = set()
        common = 0
        
        for i in range(n):
            seen_a.add(A[i])
            seen_b.add(B[i])
            
            # If A[i] was already in B, increment common
            if A[i] in seen_b:
                common += 1
            # If B[i] was already in A (and B[i] != A[i]), increment common
            if B[i] in seen_a and B[i] != A[i]:
                common += 1
            
            result.append(common)
        
        return result

Source