We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 795ec4d commit cfb087fCopy full SHA for cfb087f
leetcode/daily/2657/sol.go
@@ -0,0 +1,35 @@
1
+// https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/
2
+
3
+package main
4
5
+import "fmt"
6
7
+func findThePrefixCommonArray(A []int, B []int) []int {
8
+ seen := make(map[int]bool) // To track elements seen so far
9
+ result := make([]int, len(A))
10
+ commonCount := 0
11
12
+ for i := 0; i < len(A); i++ {
13
+ if seen[A[i]] {
14
+ commonCount++
15
+ } else {
16
+ seen[A[i]] = true
17
+ }
18
19
+ if seen[B[i]] {
20
21
22
+ seen[B[i]] = true
23
24
25
+ result[i] = commonCount
26
27
28
+ return result
29
+}
30
31
+func main() {
32
+ A := []int{1, 3, 2, 4}
33
+ B := []int{3, 1, 2, 4}
34
+ fmt.Println(findThePrefixCommonArray(A, B))
35
0 commit comments