File tree 3 files changed +27
-54
lines changed
solution/0600-0699/0648.Replace Words
3 files changed +27
-54
lines changed Original file line number Diff line number Diff line change @@ -264,36 +264,27 @@ func replaceWords(dictionary []string, sentence string) string {
264
264
265
265
``` ts
266
266
class Trie {
267
- private children: Trie [];
268
- private ref: number ;
269
-
270
- constructor () {
271
- this .children = new Array <Trie >(26 );
272
- this .ref = - 1 ;
273
- }
267
+ #children: Record <string , Trie > = {};
268
+ #ref = - 1 ;
274
269
275
270
public insert(w : string , i : number ) {
276
271
let node: Trie = this ;
277
272
for (const c of w ) {
278
- const idx = c .charCodeAt (0 ) - 97 ;
279
- if (! node .children [idx ]) {
280
- node .children [idx ] = new Trie ();
281
- }
282
- node = node .children [idx ];
273
+ node .#children [c ] ?? = new Trie ();
274
+ node = node .#children [c ];
283
275
}
284
- node .ref = i ;
276
+ node .# ref = i ;
285
277
}
286
278
287
279
public search(w : string ): number {
288
280
let node: Trie = this ;
289
281
for (const c of w ) {
290
- const idx = c .charCodeAt (0 ) - 97 ;
291
- if (! node .children [idx ]) {
282
+ if (! node .#children [c ]) {
292
283
return - 1 ;
293
284
}
294
- node = node .children [idx ];
295
- if (node .ref !== - 1 ) {
296
- return node .ref ;
285
+ node = node .# children [c ];
286
+ if (node .# ref !== - 1 ) {
287
+ return node .# ref ;
297
288
}
298
289
}
299
290
return - 1 ;
Original file line number Diff line number Diff line change @@ -251,36 +251,27 @@ func replaceWords(dictionary []string, sentence string) string {
251
251
252
252
``` ts
253
253
class Trie {
254
- private children: Trie [];
255
- private ref: number ;
256
-
257
- constructor () {
258
- this .children = new Array <Trie >(26 );
259
- this .ref = - 1 ;
260
- }
254
+ #children: Record <string , Trie > = {};
255
+ #ref = - 1 ;
261
256
262
257
public insert(w : string , i : number ) {
263
258
let node: Trie = this ;
264
259
for (const c of w ) {
265
- const idx = c .charCodeAt (0 ) - 97 ;
266
- if (! node .children [idx ]) {
267
- node .children [idx ] = new Trie ();
268
- }
269
- node = node .children [idx ];
260
+ node .#children [c ] ?? = new Trie ();
261
+ node = node .#children [c ];
270
262
}
271
- node .ref = i ;
263
+ node .# ref = i ;
272
264
}
273
265
274
266
public search(w : string ): number {
275
267
let node: Trie = this ;
276
268
for (const c of w ) {
277
- const idx = c .charCodeAt (0 ) - 97 ;
278
- if (! node .children [idx ]) {
269
+ if (! node .#children [c ]) {
279
270
return - 1 ;
280
271
}
281
- node = node .children [idx ];
282
- if (node .ref !== - 1 ) {
283
- return node .ref ;
272
+ node = node .# children [c ];
273
+ if (node .# ref !== - 1 ) {
274
+ return node .# ref ;
284
275
}
285
276
}
286
277
return - 1 ;
Original file line number Diff line number Diff line change 1
1
class Trie {
2
- private children : Trie [ ] ;
3
- private ref : number ;
4
-
5
- constructor ( ) {
6
- this . children = new Array < Trie > ( 26 ) ;
7
- this . ref = - 1 ;
8
- }
2
+ #children: Record < string , Trie > = { } ;
3
+ #ref = - 1 ;
9
4
10
5
public insert ( w : string , i : number ) {
11
6
let node : Trie = this ;
12
7
for ( const c of w ) {
13
- const idx = c . charCodeAt ( 0 ) - 97 ;
14
- if ( ! node . children [ idx ] ) {
15
- node . children [ idx ] = new Trie ( ) ;
16
- }
17
- node = node . children [ idx ] ;
8
+ node . #children[ c ] ??= new Trie ( ) ;
9
+ node = node . #children[ c ] ;
18
10
}
19
- node . ref = i ;
11
+ node . # ref = i ;
20
12
}
21
13
22
14
public search ( w : string ) : number {
23
15
let node : Trie = this ;
24
16
for ( const c of w ) {
25
- const idx = c . charCodeAt ( 0 ) - 97 ;
26
- if ( ! node . children [ idx ] ) {
17
+ if ( ! node . #children[ c ] ) {
27
18
return - 1 ;
28
19
}
29
- node = node . children [ idx ] ;
30
- if ( node . ref !== - 1 ) {
31
- return node . ref ;
20
+ node = node . # children[ c ] ;
21
+ if ( node . # ref !== - 1 ) {
22
+ return node . # ref;
32
23
}
33
24
}
34
25
return - 1 ;
You can’t perform that action at this time.
0 commit comments