diff --git a/2657. Find the Prefix Common Array of Two Arrays b/2657. Find the Prefix Common Array of Two Arrays new file mode 100644 index 0000000..0346001 --- /dev/null +++ b/2657. Find the Prefix Common Array of Two Arrays @@ -0,0 +1,15 @@ +class Solution { +public: + vector findThePrefixCommonArray(vector& A, vector& B) { + int n = A.size(), common = 0; + vector result(n), count(n + 1, 0); + + for (int i = 0; i < n; ++i) { + if (++count[A[i]] == 2) ++common; + if (++count[B[i]] == 2) ++common; + result[i] = common; + } + + return result; + } +};