deno.land / x / masx200_leetcode_test@10.6.5 / implement-trie-prefix-tree / Trie.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Trie { #words = new Set<string>(); #prefixs = new Set<string>();
insert(word: string): void { this.#words.add(word); let pre = word; while (pre) { if (this.#prefixs.has(pre)) { break; } this.#prefixs.add(pre); pre = pre.slice(0, -1); } }
search(word: string): boolean { return this.#words.has(word); }
startsWith(prefix: string): boolean { return this.#prefixs.has(prefix); }}export default Trie;
masx200_leetcode_test

Version Info

Tagged at
a year ago