Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2657
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 committed Jan 14, 2025
1 parent cd0409a commit d87ab44
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,60 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {

<!-- solution:end -->

<!-- solution:start -->

### Solution 3: Counting + Set

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$.

<!-- tabs:start -->

#### TypeScript

```ts
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
const n = A.length;
const res = Array(n).fill(0);
const [setA, setB] = [new Set<number>(), new Set<number>()];

for (let i = 0, c = 0; i < n; i++) {
if (setA.has(B[i])) c++;
if (setB.has(A[i])) c++;
if (A[i] === B[i]) c++;

setA.add(A[i]);
setB.add(B[i]);
res[i] = c;
}

return res;
}
```

#### JavaScript

```js
function findThePrefixCommonArray(A, B) {
const n = A.length;
const res = Array(n).fill(0);
const [setA, setB] = [new Set(), new Set()];

for (let i = 0, c = 0; i < n; i++) {
if (setA.has(B[i])) c++;
if (setB.has(A[i])) c++;
if (A[i] === B[i]) c++;

setA.add(A[i]);
setB.add(B[i]);
res[i] = c;
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,60 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {

<!-- solution:end -->

<!-- solution:start -->

### Solution 3: Counting + Set

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$.

<!-- tabs:start -->

#### TypeScript

```ts
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
const n = A.length;
const res = Array(n).fill(0);
const [setA, setB] = [new Set<number>(), new Set<number>()];

for (let i = 0, c = 0; i < n; i++) {
if (setA.has(B[i])) c++;
if (setB.has(A[i])) c++;
if (A[i] === B[i]) c++;

setA.add(A[i]);
setB.add(B[i]);
res[i] = c;
}

return res;
}
```

#### JavaScript

```js
function findThePrefixCommonArray(A, B) {
const n = A.length;
const res = Array(n).fill(0);
const [setA, setB] = [new Set(), new Set()];

for (let i = 0, c = 0; i < n; i++) {
if (setA.has(B[i])) c++;
if (setB.has(A[i])) c++;
if (A[i] === B[i]) c++;

setA.add(A[i]);
setB.add(B[i]);
res[i] = c;
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function findThePrefixCommonArray(A, B) {
const n = A.length;
const res = Array(n).fill(0);
const [setA, setB] = [new Set(), new Set()];

for (let i = 0, c = 0; i < n; i++) {
if (setA.has(B[i])) c++;
if (setB.has(A[i])) c++;
if (A[i] === B[i]) c++;

setA.add(A[i]);
setB.add(B[i]);
res[i] = c;
}

return res;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
const n = A.length;
const res = Array(n).fill(0);
const [setA, setB] = [new Set<number>(), new Set<number>()];

for (let i = 0, c = 0; i < n; i++) {
if (setA.has(B[i])) c++;
if (setB.has(A[i])) c++;
if (A[i] === B[i]) c++;

setA.add(A[i]);
setB.add(B[i]);
res[i] = c;
}

return res;
}

0 comments on commit d87ab44

Please sign in to comment.