Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2657 #3952

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}