-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind the prefix common array of two arrays.py
36 lines (29 loc) · 1.31 KB
/
Find the prefix common array of two arrays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution:
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
# Get the length of the input arrays A and B
n = len(A)
# Initialize the set to keep track of elements we have seen so far
seen = set()
# Variable to keep track of the count of the common elements seen in the prefixes of A and B
C = [0] * n
# Iterate through each index of the arrays
commonCount = 0
for i in range(n):
# Check if the current element of A is already in the 'seen' set
if A[i] in seen:
# If it is, increment the count of the common elements
commonCount += 1
else:
# Otherwise, add it to the 'seen' set
seen.add(A[i])
# Check if the current element of B is already in the 'seen' set
if B[i] in seen:
# If it is, increment the count of common elements
commonCount += 1
else:
# Otherwise, add it to the 'seen' set
seen.add(B[i])
# Update the current index of C with the current count of common elements
C[i] = commonCount
# Return the resulting array C
return C