diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md index 16f40d1bbc983..d211ced089ca7 100644 --- a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md +++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md @@ -176,6 +176,54 @@ public class Solution { } ``` +#### TypeScript + +```ts +export function countPalindromicSubsequence(s: string): number { + 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; +} +``` + +#### 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; +} +``` + diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md index 7a2ace64876ac..98f8bacde694b 100644 --- a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md +++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md @@ -168,6 +168,54 @@ public class Solution { } ``` +#### TypeScript + +```ts +export function countPalindromicSubsequence(s: string): number { + 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; +} +``` + +#### 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; +} +``` + diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.js b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.js new file mode 100644 index 0000000000000..be91880d3c561 --- /dev/null +++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.js @@ -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; +} diff --git a/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.ts b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.ts new file mode 100644 index 0000000000000..2602edafef8e6 --- /dev/null +++ b/solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/Solution.ts @@ -0,0 +1,19 @@ +export function countPalindromicSubsequence(s: string): number { + 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; +}