Skip to content

Commit 8f715d6

Browse files
committedJun 7, 2024
refactor: update ts solution to lc problem: No.0846
1 parent 1da0998 commit 8f715d6

File tree

3 files changed

+27
-54
lines changed

3 files changed

+27
-54
lines changed
 

‎solution/0600-0699/0648.Replace Words/README.md

+9-18
Original file line numberDiff line numberDiff line change
@@ -264,36 +264,27 @@ func replaceWords(dictionary []string, sentence string) string {
264264

265265
```ts
266266
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;
274269

275270
public insert(w: string, i: number) {
276271
let node: Trie = this;
277272
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];
283275
}
284-
node.ref = i;
276+
node.#ref = i;
285277
}
286278

287279
public search(w: string): number {
288280
let node: Trie = this;
289281
for (const c of w) {
290-
const idx = c.charCodeAt(0) - 97;
291-
if (!node.children[idx]) {
282+
if (!node.#children[c]) {
292283
return -1;
293284
}
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;
297288
}
298289
}
299290
return -1;

‎solution/0600-0699/0648.Replace Words/README_EN.md

+9-18
Original file line numberDiff line numberDiff line change
@@ -251,36 +251,27 @@ func replaceWords(dictionary []string, sentence string) string {
251251

252252
```ts
253253
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;
261256

262257
public insert(w: string, i: number) {
263258
let node: Trie = this;
264259
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];
270262
}
271-
node.ref = i;
263+
node.#ref = i;
272264
}
273265

274266
public search(w: string): number {
275267
let node: Trie = this;
276268
for (const c of w) {
277-
const idx = c.charCodeAt(0) - 97;
278-
if (!node.children[idx]) {
269+
if (!node.#children[c]) {
279270
return -1;
280271
}
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;
284275
}
285276
}
286277
return -1;

‎solution/0600-0699/0648.Replace Words/Solution.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
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;
94

105
public insert(w: string, i: number) {
116
let node: Trie = this;
127
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];
1810
}
19-
node.ref = i;
11+
node.#ref = i;
2012
}
2113

2214
public search(w: string): number {
2315
let node: Trie = this;
2416
for (const c of w) {
25-
const idx = c.charCodeAt(0) - 97;
26-
if (!node.children[idx]) {
17+
if (!node.#children[c]) {
2718
return -1;
2819
}
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;
3223
}
3324
}
3425
return -1;

0 commit comments

Comments
 (0)