Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a1dc53

Browse files
authored
Create 2657. Find the Prefix Common Array of Two Arrays
1 parent 803dd53 commit 5a1dc53

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
3+
int[] c = new int[A.length];
4+
for(int i=0;i<A.length;i++){
5+
boolean found=false;
6+
for(int j=0;j<B.length;j++){
7+
if(A[i]==B[j]){
8+
found=true;
9+
}
10+
if(found && j>=i){
11+
c[j]++;
12+
}
13+
}
14+
}
15+
return c;
16+
}
17+
}
18+
19+
20+
class Solution {
21+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
22+
int[] c = new int[A.length];
23+
int[] freq = new int[A.length+1];
24+
int count=0;
25+
for(int i=0;i<A.length;i++){
26+
freq[A[i]]++;
27+
if(freq[A[i]]==2) count++;
28+
freq[B[i]]++;
29+
if(freq[B[i]]==2) count++;
30+
c[i] = count;
31+
}
32+
return c;
33+
}
34+
}

0 commit comments

Comments
 (0)