[translate by ChatGPT] Just maintain two caches, count if the character B[i] is in cache of A, and do the same thing reversely.

Code

class Solution {
public:
    vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
        vector<bool> cacheA(50, false);
        vector<bool> cacheB(50, false);
        int ans = 0;
        vector<int> ansAry;
        for(int i = 0 ; i < A.size() ; ++i) {
            if(cacheA[B[i]]) ++ans;
            if(cacheB[A[i]]) ++ans;
            if(A[i] == B[i]) ++ans;
            cacheA[A[i]] = true;
            cacheB[B[i]] = true;
            ansAry.push_back(ans);
        }
        return ansAry;
    }
};