Skip to content

Commit d87ab44

Browse files
committed
feat: add solutions to lc problem: No.2657
1 parent cd0409a commit d87ab44

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,60 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {
300300

301301
<!-- solution:end -->
302302

303+
<!-- solution:start -->
304+
305+
### Solution 3: Counting + Set
306+
307+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$.
308+
309+
<!-- tabs:start -->
310+
311+
#### TypeScript
312+
313+
```ts
314+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
315+
const n = A.length;
316+
const res = Array(n).fill(0);
317+
const [setA, setB] = [new Set<number>(), new Set<number>()];
318+
319+
for (let i = 0, c = 0; i < n; i++) {
320+
if (setA.has(B[i])) c++;
321+
if (setB.has(A[i])) c++;
322+
if (A[i] === B[i]) c++;
323+
324+
setA.add(A[i]);
325+
setB.add(B[i]);
326+
res[i] = c;
327+
}
328+
329+
return res;
330+
}
331+
```
332+
333+
#### JavaScript
334+
335+
```js
336+
function findThePrefixCommonArray(A, B) {
337+
const n = A.length;
338+
const res = Array(n).fill(0);
339+
const [setA, setB] = [new Set(), new Set()];
340+
341+
for (let i = 0, c = 0; i < n; i++) {
342+
if (setA.has(B[i])) c++;
343+
if (setB.has(A[i])) c++;
344+
if (A[i] === B[i]) c++;
345+
346+
setA.add(A[i]);
347+
setB.add(B[i]);
348+
res[i] = c;
349+
}
350+
351+
return res;
352+
}
353+
```
354+
355+
<!-- tabs:end -->
356+
357+
<!-- solution:end -->
358+
303359
<!-- problem:end -->

solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README_EN.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,60 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {
300300

301301
<!-- solution:end -->
302302

303+
<!-- solution:start -->
304+
305+
### Solution 3: Counting + Set
306+
307+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$.
308+
309+
<!-- tabs:start -->
310+
311+
#### TypeScript
312+
313+
```ts
314+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
315+
const n = A.length;
316+
const res = Array(n).fill(0);
317+
const [setA, setB] = [new Set<number>(), new Set<number>()];
318+
319+
for (let i = 0, c = 0; i < n; i++) {
320+
if (setA.has(B[i])) c++;
321+
if (setB.has(A[i])) c++;
322+
if (A[i] === B[i]) c++;
323+
324+
setA.add(A[i]);
325+
setB.add(B[i]);
326+
res[i] = c;
327+
}
328+
329+
return res;
330+
}
331+
```
332+
333+
#### JavaScript
334+
335+
```js
336+
function findThePrefixCommonArray(A, B) {
337+
const n = A.length;
338+
const res = Array(n).fill(0);
339+
const [setA, setB] = [new Set(), new Set()];
340+
341+
for (let i = 0, c = 0; i < n; i++) {
342+
if (setA.has(B[i])) c++;
343+
if (setB.has(A[i])) c++;
344+
if (A[i] === B[i]) c++;
345+
346+
setA.add(A[i]);
347+
setB.add(B[i]);
348+
res[i] = c;
349+
}
350+
351+
return res;
352+
}
353+
```
354+
355+
<!-- tabs:end -->
356+
357+
<!-- solution:end -->
358+
303359
<!-- problem:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function findThePrefixCommonArray(A, B) {
2+
const n = A.length;
3+
const res = Array(n).fill(0);
4+
const [setA, setB] = [new Set(), new Set()];
5+
6+
for (let i = 0, c = 0; i < n; i++) {
7+
if (setA.has(B[i])) c++;
8+
if (setB.has(A[i])) c++;
9+
if (A[i] === B[i]) c++;
10+
11+
setA.add(A[i]);
12+
setB.add(B[i]);
13+
res[i] = c;
14+
}
15+
16+
return res;
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
2+
const n = A.length;
3+
const res = Array(n).fill(0);
4+
const [setA, setB] = [new Set<number>(), new Set<number>()];
5+
6+
for (let i = 0, c = 0; i < n; i++) {
7+
if (setA.has(B[i])) c++;
8+
if (setB.has(A[i])) c++;
9+
if (A[i] === B[i]) c++;
10+
11+
setA.add(A[i]);
12+
setB.add(B[i]);
13+
res[i] = c;
14+
}
15+
16+
return res;
17+
}

0 commit comments

Comments
 (0)