Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b640da

Browse files
authored
refactor: update ts solution to lc problem: No.0846 (doocs#3074)
1 parent 50b8df8 commit 1b640da

File tree

3 files changed

+33
-60
lines changed

3 files changed

+33
-60
lines changed

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

Lines changed: 11 additions & 20 deletions
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-
privatechildren: Trie[];
268-
privateref:number;
267+
#children: Record<string, Trie> = {};
268+
#ref=-1;
269269

270-
constructor() {
271-
this.children = new Array<Trie>(26);
272-
this.ref = -1;
273-
}
274-
275-
public insert(w: string, i: number) {
270+
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

287-
publicsearch(w: string): number {
279+
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‎

Lines changed: 11 additions & 20 deletions
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-
privatechildren: Trie[];
255-
privateref:number;
254+
#children: Record<string, Trie> = {};
255+
#ref=-1;
256256

257-
constructor() {
258-
this.children = new Array<Trie>(26);
259-
this.ref = -1;
260-
}
261-
262-
public insert(w: string, i: number) {
257+
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

274-
publicsearch(w: string): number {
266+
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‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
class Trie {
2-
privatechildren: Trie[];
3-
privateref: number;
2+
#children: Record<string,Trie>={};
3+
#ref=-1;
44

5-
constructor() {
6-
this.children = new Array<Trie>(26);
7-
this.ref = -1;
8-
}
9-
10-
public insert(w: string, i: number) {
5+
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

22-
publicsearch(w: string): number {
14+
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)

AltStyle によって変換されたページ (->オリジナル) /