Skip to content

refactor: update ts solution to lc problem: No.0846 #3074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions solution/0600-0699/0648.Replace Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,36 +264,27 @@ func replaceWords(dictionary []string, sentence string) string {

```ts
class Trie {
private children: Trie[];
private ref: number;
#children: Record<string, Trie> = {};
#ref = -1;

constructor() {
this.children = new Array<Trie>(26);
this.ref = -1;
}

public insert(w: string, i: number) {
insert(w: string, i: number) {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
node.children[idx] = new Trie();
}
node = node.children[idx];
node.#children[c] ??= new Trie();
node = node.#children[c];
}
node.ref = i;
node.#ref = i;
}

public search(w: string): number {
search(w: string): number {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
if (!node.#children[c]) {
return -1;
}
node = node.children[idx];
if (node.ref !== -1) {
return node.ref;
node = node.#children[c];
if (node.#ref !== -1) {
return node.#ref;
}
}
return -1;
Expand Down
31 changes: 11 additions & 20 deletions solution/0600-0699/0648.Replace Words/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,36 +251,27 @@ func replaceWords(dictionary []string, sentence string) string {

```ts
class Trie {
private children: Trie[];
private ref: number;
#children: Record<string, Trie> = {};
#ref = -1;

constructor() {
this.children = new Array<Trie>(26);
this.ref = -1;
}

public insert(w: string, i: number) {
insert(w: string, i: number) {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
node.children[idx] = new Trie();
}
node = node.children[idx];
node.#children[c] ??= new Trie();
node = node.#children[c];
}
node.ref = i;
node.#ref = i;
}

public search(w: string): number {
search(w: string): number {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
if (!node.#children[c]) {
return -1;
}
node = node.children[idx];
if (node.ref !== -1) {
return node.ref;
node = node.#children[c];
if (node.#ref !== -1) {
return node.#ref;
}
}
return -1;
Expand Down
31 changes: 11 additions & 20 deletions solution/0600-0699/0648.Replace Words/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
class Trie {
private children: Trie[];
private ref: number;
#children: Record<string, Trie> = {};
#ref = -1;

constructor() {
this.children = new Array<Trie>(26);
this.ref = -1;
}

public insert(w: string, i: number) {
insert(w: string, i: number) {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
node.children[idx] = new Trie();
}
node = node.children[idx];
node.#children[c] ??= new Trie();
node = node.#children[c];
}
node.ref = i;
node.#ref = i;
}

public search(w: string): number {
search(w: string): number {
let node: Trie = this;
for (const c of w) {
const idx = c.charCodeAt(0) - 97;
if (!node.children[idx]) {
if (!node.#children[c]) {
return -1;
}
node = node.children[idx];
if (node.ref !== -1) {
return node.ref;
node = node.#children[c];
if (node.#ref !== -1) {
return node.#ref;
}
}
return -1;
Expand Down
Loading