File tree Expand file tree Collapse file tree 3 files changed +141
-15
lines changed
solution/2400-2499/2416.Sum of Prefix Scores of Strings Expand file tree Collapse file tree 3 files changed +141
-15
lines changed Original file line number Diff line number Diff line change @@ -290,6 +290,56 @@ function sumPrefixScores(words: string[]): number[] {
290
290
}
291
291
```
292
292
293
+ ``` ts
294
+ class Trie {
295
+ children: Array <any >;
296
+ cnt: number ;
297
+
298
+ constructor () {
299
+ this .children = Array (26 );
300
+ this .cnt = 0 ;
301
+ }
302
+
303
+ insert(w : string ): void {
304
+ let node = this ;
305
+ for (const c of w ) {
306
+ const idx = c .charCodeAt (0 ) - ' a' .charCodeAt (0 );
307
+ if (! node .children [idx ]) {
308
+ node .children [idx ] = new Trie ();
309
+ }
310
+ node = node .children [idx ];
311
+ node .cnt ++ ;
312
+ }
313
+ }
314
+
315
+ search(w : string ): number {
316
+ let node = this ;
317
+ let ans = 0 ;
318
+ for (const c of w ) {
319
+ const idx = c .charCodeAt (0 ) - ' a' .charCodeAt (0 );
320
+ if (! node .children [idx ]) {
321
+ return ans ;
322
+ }
323
+ node = node .children [idx ];
324
+ ans += node .cnt ;
325
+ }
326
+ return ans ;
327
+ }
328
+ }
329
+
330
+ function sumPrefixScores(words : string []): number [] {
331
+ const trie = new Trie ();
332
+ for (const w of words ) {
333
+ trie .insert (w );
334
+ }
335
+ let ans = [];
336
+ for (const w of words ) {
337
+ ans .push (trie .search (w ));
338
+ }
339
+ return ans ;
340
+ }
341
+ ```
342
+
293
343
### ** ...**
294
344
295
345
```
Original file line number Diff line number Diff line change @@ -274,6 +274,56 @@ function sumPrefixScores(words: string[]): number[] {
274
274
}
275
275
```
276
276
277
+ ``` ts
278
+ class Trie {
279
+ children: Array <any >;
280
+ cnt: number ;
281
+
282
+ constructor () {
283
+ this .children = Array (26 );
284
+ this .cnt = 0 ;
285
+ }
286
+
287
+ insert(w : string ): void {
288
+ let node = this ;
289
+ for (const c of w ) {
290
+ const idx = c .charCodeAt (0 ) - ' a' .charCodeAt (0 );
291
+ if (! node .children [idx ]) {
292
+ node .children [idx ] = new Trie ();
293
+ }
294
+ node = node .children [idx ];
295
+ node .cnt ++ ;
296
+ }
297
+ }
298
+
299
+ search(w : string ): number {
300
+ let node = this ;
301
+ let ans = 0 ;
302
+ for (const c of w ) {
303
+ const idx = c .charCodeAt (0 ) - ' a' .charCodeAt (0 );
304
+ if (! node .children [idx ]) {
305
+ return ans ;
306
+ }
307
+ node = node .children [idx ];
308
+ ans += node .cnt ;
309
+ }
310
+ return ans ;
311
+ }
312
+ }
313
+
314
+ function sumPrefixScores(words : string []): number [] {
315
+ const trie = new Trie ();
316
+ for (const w of words ) {
317
+ trie .insert (w );
318
+ }
319
+ let ans = [];
320
+ for (const w of words ) {
321
+ ans .push (trie .search (w ));
322
+ }
323
+ return ans ;
324
+ }
325
+ ```
326
+
277
327
### ** ...**
278
328
279
329
```
Original file line number Diff line number Diff line change 1
- function sumPrefixScores ( words : string [ ] ) : number [ ] {
2
- const map = new Map ( ) ;
1
+ class Trie {
2
+ children : Array < any > ;
3
+ cnt : number ;
4
+
5
+ constructor ( ) {
6
+ this . children = Array ( 26 ) ;
7
+ this . cnt = 0 ;
8
+ }
3
9
4
- for ( const word of words ) {
5
- const n = word . length ;
6
- for ( let i = 1 ; i <= n ; i ++ ) {
7
- const s = word . slice ( 0 , i ) ;
8
- map . set ( s , ( map . get ( s ) ?? 0 ) + 1 ) ;
10
+ insert ( w : string ) : void {
11
+ let node = this ;
12
+ for ( const c of w ) {
13
+ const idx = c . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
14
+ if ( ! node . children [ idx ] ) {
15
+ node . children [ idx ] = new Trie ( ) ;
16
+ }
17
+ node = node . children [ idx ] ;
18
+ node . cnt ++ ;
9
19
}
10
20
}
11
21
12
- return words . map ( word => {
13
- const n = word . length ;
14
- let count = 0 ;
15
- for ( let i = 1 ; i <= n ; i ++ ) {
16
- const s = word . slice ( 0 , i ) ;
17
- count += map . get ( s ) ;
22
+ search ( w : string ) : number {
23
+ let node = this ;
24
+ let ans = 0 ;
25
+ for ( const c of w ) {
26
+ const idx = c . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ;
27
+ if ( ! node . children [ idx ] ) {
28
+ return ans ;
29
+ }
30
+ node = node . children [ idx ] ;
31
+ ans += node . cnt ;
18
32
}
19
- return count ;
20
- } ) ;
33
+ return ans ;
34
+ }
35
+ }
36
+
37
+ function sumPrefixScores ( words : string [ ] ) : number [ ] {
38
+ const trie = new Trie ( ) ;
39
+ for ( const w of words ) {
40
+ trie . insert ( w ) ;
41
+ }
42
+ let ans = [ ] ;
43
+ for ( const w of words ) {
44
+ ans . push ( trie . search ( w ) ) ;
45
+ }
46
+ return ans ;
21
47
}
You can’t perform that action at this time.
0 commit comments