File tree Expand file tree Collapse file tree 4 files changed +134
-0
lines changed
solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences Expand file tree Collapse file tree 4 files changed +134
-0
lines changed Original file line number Diff line number Diff line change @@ -176,6 +176,54 @@ public class Solution {
176176}
177177```
178178
179+ #### TypeScript
180+
181+ ``` ts
182+ export function countPalindromicSubsequence(s : string ): number {
183+ const cnt = new Map <string , [number , number ]>();
184+ const n = s .length ;
185+ let ans = 0 ;
186+
187+ for (let i = 0 ; i < n ; i ++ ) {
188+ const ch = s [i ];
189+ if (cnt .has (ch )) cnt .get (ch )! [1 ] = i ;
190+ else cnt .set (ch , [i , i ]);
191+ }
192+
193+ for (const [_, [i, j]] of cnt ) {
194+ if (i !== j ) {
195+ ans += new Set (s .slice (i + 1 , j )).size ;
196+ }
197+ }
198+
199+ return ans ;
200+ }
201+ ```
202+
203+ #### JavaScript
204+
205+ ``` js
206+ export function countPalindromicSubsequence (s ) {
207+ const cnt = new Map ();
208+ const n = s .length ;
209+ let ans = 0 ;
210+
211+ for (let i = 0 ; i < n; i++ ) {
212+ const ch = s[i];
213+ if (cnt .has (ch)) cnt .get (ch)[1 ] = i;
214+ else cnt .set (ch, [i, i]);
215+ }
216+
217+ for (const [_ , [i , j ]] of cnt) {
218+ if (i !== j) {
219+ ans += new Set (s .slice (i + 1 , j)).size ;
220+ }
221+ }
222+
223+ return ans;
224+ }
225+ ```
226+
179227<!-- tabs: end -->
180228
181229<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -168,6 +168,54 @@ public class Solution {
168168}
169169```
170170
171+ #### TypeScript
172+
173+ ``` ts
174+ export function countPalindromicSubsequence(s : string ): number {
175+ const cnt = new Map <string , [number , number ]>();
176+ const n = s .length ;
177+ let ans = 0 ;
178+
179+ for (let i = 0 ; i < n ; i ++ ) {
180+ const ch = s [i ];
181+ if (cnt .has (ch )) cnt .get (ch )! [1 ] = i ;
182+ else cnt .set (ch , [i , i ]);
183+ }
184+
185+ for (const [_, [i, j]] of cnt ) {
186+ if (i !== j ) {
187+ ans += new Set (s .slice (i + 1 , j )).size ;
188+ }
189+ }
190+
191+ return ans ;
192+ }
193+ ```
194+
195+ #### JavaScript
196+
197+ ``` js
198+ export function countPalindromicSubsequence (s ) {
199+ const cnt = new Map ();
200+ const n = s .length ;
201+ let ans = 0 ;
202+
203+ for (let i = 0 ; i < n; i++ ) {
204+ const ch = s[i];
205+ if (cnt .has (ch)) cnt .get (ch)[1 ] = i;
206+ else cnt .set (ch, [i, i]);
207+ }
208+
209+ for (const [_ , [i , j ]] of cnt) {
210+ if (i !== j) {
211+ ans += new Set (s .slice (i + 1 , j)).size ;
212+ }
213+ }
214+
215+ return ans;
216+ }
217+ ```
218+
171219<!-- tabs: end -->
172220
173221<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ export function countPalindromicSubsequence ( s ) {
2+ const cnt = new Map ( ) ;
3+ const n = s . length ;
4+ let ans = 0 ;
5+
6+ for ( let i = 0 ; i < n ; i ++ ) {
7+ const ch = s [ i ] ;
8+ if ( cnt . has ( ch ) ) cnt . get ( ch ) [ 1 ] = i ;
9+ else cnt . set ( ch , [ i , i ] ) ;
10+ }
11+
12+ for ( const [ _ , [ i , j ] ] of cnt ) {
13+ if ( i !== j ) {
14+ ans += new Set ( s . slice ( i + 1 , j ) ) . size ;
15+ }
16+ }
17+
18+ return ans ;
19+ }
Original file line number Diff line number Diff line change 1+ export function countPalindromicSubsequence ( s : string ) : number {
2+ const cnt = new Map < string , [ number , number ] > ( ) ;
3+ const n = s . length ;
4+ let ans = 0 ;
5+
6+ for ( let i = 0 ; i < n ; i ++ ) {
7+ const ch = s [ i ] ;
8+ if ( cnt . has ( ch ) ) cnt . get ( ch ) ! [ 1 ] = i ;
9+ else cnt . set ( ch , [ i , i ] ) ;
10+ }
11+
12+ for ( const [ _ , [ i , j ] ] of cnt ) {
13+ if ( i !== j ) {
14+ ans += new Set ( s . slice ( i + 1 , j ) ) . size ;
15+ }
16+ }
17+
18+ return ans ;
19+ }
You can’t perform that action at this time.
0 commit comments