---
# System prepended metadata

title: 2657. Find the Prefix Common Array of Two Arrays
tags: [Leetcode, HashMap, Medium]

---

# 2657. Find the Prefix Common Array of Two Arrays
###### tags: `Leetcode` `Medium` `HashMap`
## 思路
一开始的思路是遍历的时候 在算完两个array的counter之后 用回圈遍历counter array找到答案
但后来发现ans array里面的数字一定是nondecreasing的 因此我们只要想办法加上每次的变化量即可 而每次的变化量就跟当前遍历到的数字有关
## Code
```python=
class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        counterA, counterB = [0]*51, [0]*51
        ans = [0]*len(A)
        count = 0
        for i in range(len(A)):
            if A[i]==B[i]:
                count += 1
            else:
                if counterA[A[i]]<counterB[A[i]]: count += 1
                if counterA[B[i]]>counterB[B[i]]: count += 1
            counterA[A[i]] += 1
            counterB[B[i]] += 1
            ans[i] = count
        return ans
```
