File tree 3 files changed +124
-0
lines changed
solution/0600-0699/0648.Replace Words
3 files changed +124
-0
lines changed Original file line number Diff line number Diff line change @@ -376,6 +376,49 @@ class Solution {
376
376
}
377
377
```
378
378
379
+ #### TypeScript
380
+
381
+ ``` ts
382
+ function replaceWords(dictionary : string [], sentence : string ): string {
383
+ const words = sentence .split (' ' );
384
+ const trie: Trie = {};
385
+ const TERMINAL_MARK = ' TERMINAL_MARK' ;
386
+
387
+ for (const s of dictionary ) {
388
+ let t = trie ;
389
+
390
+ for (const ch of s ) {
391
+ t [ch ] ?? = {};
392
+ t = t [ch ] as Trie_ ;
393
+ }
394
+ t [TERMINAL_MARK ] = TERMINAL_MARK ;
395
+ }
396
+
397
+ for (let i = 0 ; i < words .length ; i ++ ) {
398
+ const s = words [i ];
399
+ let t = trie ;
400
+
401
+ for (let j = 0 ; j < s .length ; j ++ ) {
402
+ const ch = s [j ];
403
+
404
+ if (! t [ch ]) break ;
405
+
406
+ if ((t [ch ] as Trie_ )[TERMINAL_MARK ]) {
407
+ words [i ] = s .slice (0 , j + 1 );
408
+ break ;
409
+ }
410
+ t = t [ch ] as Trie_ ;
411
+ }
412
+ }
413
+
414
+ return words .join (' ' );
415
+ }
416
+
417
+ // prettier-ignore
418
+ type Trie = { [key : string ]: Trie } | string
419
+ type Trie_ = Exclude <Trie , string >;
420
+ ```
421
+
379
422
<!-- tabs:end -->
380
423
381
424
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -363,6 +363,49 @@ class Solution {
363
363
}
364
364
```
365
365
366
+ #### TypeScript
367
+
368
+ ``` ts
369
+ function replaceWords(dictionary : string [], sentence : string ): string {
370
+ const words = sentence .split (' ' );
371
+ const trie: Trie = {};
372
+ const TERMINAL_MARK = ' TERMINAL_MARK' ;
373
+
374
+ for (const s of dictionary ) {
375
+ let t = trie ;
376
+
377
+ for (const ch of s ) {
378
+ t [ch ] ?? = {};
379
+ t = t [ch ] as Trie_ ;
380
+ }
381
+ t [TERMINAL_MARK ] = TERMINAL_MARK ;
382
+ }
383
+
384
+ for (let i = 0 ; i < words .length ; i ++ ) {
385
+ const s = words [i ];
386
+ let t = trie ;
387
+
388
+ for (let j = 0 ; j < s .length ; j ++ ) {
389
+ const ch = s [j ];
390
+
391
+ if (! t [ch ]) break ;
392
+
393
+ if ((t [ch ] as Trie_ )[TERMINAL_MARK ]) {
394
+ words [i ] = s .slice (0 , j + 1 );
395
+ break ;
396
+ }
397
+ t = t [ch ] as Trie_ ;
398
+ }
399
+ }
400
+
401
+ return words .join (' ' );
402
+ }
403
+
404
+ // prettier-ignore
405
+ type Trie = { [key : string ]: Trie } | string
406
+ type Trie_ = Exclude <Trie , string >;
407
+ ```
408
+
366
409
<!-- tabs:end -->
367
410
368
411
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ function replaceWords ( dictionary : string [ ] , sentence : string ) : string {
2
+ const words = sentence . split ( ' ' ) ;
3
+ const trie : Trie = { } ;
4
+ const TERMINAL_MARK = 'TERMINAL_MARK' ;
5
+
6
+ for ( const s of dictionary ) {
7
+ let t = trie ;
8
+
9
+ for ( const ch of s ) {
10
+ t [ ch ] ??= { } ;
11
+ t = t [ ch ] as Trie_ ;
12
+ }
13
+ t [ TERMINAL_MARK ] = TERMINAL_MARK ;
14
+ }
15
+
16
+ for ( let i = 0 ; i < words . length ; i ++ ) {
17
+ const s = words [ i ] ;
18
+ let t = trie ;
19
+
20
+ for ( let j = 0 ; j < s . length ; j ++ ) {
21
+ const ch = s [ j ] ;
22
+
23
+ if ( ! t [ ch ] ) break ;
24
+
25
+ if ( ( t [ ch ] as Trie_ ) [ TERMINAL_MARK ] ) {
26
+ words [ i ] = s . slice ( 0 , j + 1 ) ;
27
+ break ;
28
+ }
29
+ t = t [ ch ] as Trie_ ;
30
+ }
31
+ }
32
+
33
+ return words . join ( ' ' ) ;
34
+ }
35
+
36
+ // prettier-ignore
37
+ type Trie = { [ key : string ] : Trie } | string
38
+ type Trie_ = Exclude < Trie , string > ;
You can’t perform that action at this time.
0 commit comments