Skip to content

Commit 4691d7f

Browse files
committed
https://leetcode.cn/problems/replace-words/
1 parent 5db82b2 commit 4691d7f

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ https://leetcode.cn/problems/insert-delete-getrandom-o1-duplicates-allowed/
886886

887887
https://leetcode.cn/problems/find-median-from-data-stream/
888888

889+
https://leetcode.cn/problems/replace-words/
890+
889891
#### 安装教程
890892

891893
1. 安装`deno`

design-add-and-search-words-data-structure/PrefixTreeInsert.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ export function PrefixTreeInsert<T extends PrefixTree>(
44
root: T,
55
word: string,
66
{
7+
stop,
78
end,
89
each,
910
create = () => PrefixTree() as T,
1011
}: {
12+
stop?: (node: T) => boolean;
1113
each?: (node: T) => void;
1214
create?: () => T;
1315
end?: (node: T) => void;
1416
} = {},
1517
): void {
1618
if (word.length === 0) return;
19+
1720
each?.(root);
21+
if (stop?.(root)) return;
1822
let node = root;
1923
for (const ch of word) {
2024
const next: T = (node.children.get(ch) ??
@@ -23,7 +27,9 @@ export function PrefixTreeInsert<T extends PrefixTree>(
2327
node.children.set(ch, next);
2428
return next;
2529
})()) as T;
30+
2631
each?.(next);
32+
if (stop?.(next)) return;
2733
node = next;
2834
}
2935
node.isEnd = true;

implement-trie-prefix-tree/PrefixTreeSearchPrefix.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@ export function PrefixTreeSearchPrefix<T extends PrefixTree | TrieNode>(
55
root: T,
66
prefix: string,
77
{
8+
stop,
89
end,
910
each,
1011
}: {
12+
stop?: (node: T) => boolean;
1113
each?: (node: T) => void;
1214

1315
end?: (node: T) => void;
1416
} = {},
1517
): T | undefined {
1618
let node = root;
19+
1720
each?.(root);
21+
if (stop?.(root)) return root;
1822
for (const ch of prefix) {
1923
const next: T = node.children.get(ch) as T;
2024
if (!next) {
2125
return;
2226
}
27+
2328
each?.(next);
29+
if (stop?.(next)) return next;
2430
node = next;
2531
}
2632
end?.(node);

replace-words/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { PrefixTree } from "../implement-trie-prefix-tree/PrefixTree.ts";
2+
import { PrefixTreeInsert, PrefixTreeSearchPrefix } from "../mod.ts";
3+
export default function replaceWords(
4+
dictionary: string[],
5+
sentence: string,
6+
): string {
7+
const root = PrefixTree();
8+
dictionary.forEach((word) =>
9+
PrefixTreeInsert(root, word, {
10+
stop: (node) => node.isEnd,
11+
})
12+
);
13+
14+
const words = sentence.split(" ");
15+
return words
16+
.map((word) => {
17+
let index = -1;
18+
const node = PrefixTreeSearchPrefix(root, word, {
19+
stop: (node) => node.isEnd,
20+
each() {
21+
index++;
22+
},
23+
});
24+
if (!node) {
25+
return word;
26+
}
27+
return word.slice(0, index);
28+
})
29+
.join(" ");
30+
}

replace-words/test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { assertEquals } from "../deps.ts";
2+
import replaceWords from "./index.ts";
3+
4+
Deno.test("replace-words", () => {
5+
const dictionary = ["cat", "bat", "rat"];
6+
const sentence = "the cattle was rattled by the battery";
7+
const result = replaceWords(dictionary, sentence);
8+
assertEquals(result, "the cat was rat by the bat");
9+
10+
assertEquals(
11+
"a a b c",
12+
replaceWords(["a", "b", "c"], "aadsfasf absbs bbab cadsfafs"),
13+
);
14+
});

0 commit comments

Comments
 (0)