Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.1930 (#3924)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 authored Jan 5, 2025
1 parent 2b30bd0 commit 3b89cf8
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,54 @@ public class Solution {
}
```

#### TypeScript

```ts
export function countPalindromicSubsequence(s: string): number {
const cnt = new Map<string, [number, number]>();
const n = s.length;
let ans = 0;

for (let i = 0; i < n; i++) {
const ch = s[i];
if (cnt.has(ch)) cnt.get(ch)![1] = i;
else cnt.set(ch, [i, i]);
}

for (const [_, [i, j]] of cnt) {
if (i !== j) {
ans += new Set(s.slice(i + 1, j)).size;
}
}

return ans;
}
```

#### JavaScript

```js
export function countPalindromicSubsequence(s) {
const cnt = new Map();
const n = s.length;
let ans = 0;

for (let i = 0; i < n; i++) {
const ch = s[i];
if (cnt.has(ch)) cnt.get(ch)[1] = i;
else cnt.set(ch, [i, i]);
}

for (const [_, [i, j]] of cnt) {
if (i !== j) {
ans += new Set(s.slice(i + 1, j)).size;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,54 @@ public class Solution {
}
```

#### TypeScript

```ts
export function countPalindromicSubsequence(s: string): number {
const cnt = new Map<string, [number, number]>();
const n = s.length;
let ans = 0;

for (let i = 0; i < n; i++) {
const ch = s[i];
if (cnt.has(ch)) cnt.get(ch)![1] = i;
else cnt.set(ch, [i, i]);
}

for (const [_, [i, j]] of cnt) {
if (i !== j) {
ans += new Set(s.slice(i + 1, j)).size;
}
}

return ans;
}
```

#### JavaScript

```js
export function countPalindromicSubsequence(s) {
const cnt = new Map();
const n = s.length;
let ans = 0;

for (let i = 0; i < n; i++) {
const ch = s[i];
if (cnt.has(ch)) cnt.get(ch)[1] = i;
else cnt.set(ch, [i, i]);
}

for (const [_, [i, j]] of cnt) {
if (i !== j) {
ans += new Set(s.slice(i + 1, j)).size;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function countPalindromicSubsequence(s) {
const cnt = new Map();
const n = s.length;
let ans = 0;

for (let i = 0; i < n; i++) {
const ch = s[i];
if (cnt.has(ch)) cnt.get(ch)[1] = i;
else cnt.set(ch, [i, i]);
}

for (const [_, [i, j]] of cnt) {
if (i !== j) {
ans += new Set(s.slice(i + 1, j)).size;
}
}

return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function countPalindromicSubsequence(s: string): number {
const cnt = new Map<string, [number, number]>();
const n = s.length;
let ans = 0;

for (let i = 0; i < n; i++) {
const ch = s[i];
if (cnt.has(ch)) cnt.get(ch)![1] = i;
else cnt.set(ch, [i, i]);
}

for (const [_, [i, j]] of cnt) {
if (i !== j) {
ans += new Set(s.slice(i + 1, j)).size;
}
}

return ans;
}

0 comments on commit 3b89cf8

Please sign in to comment.