deno.land / x / masx200_leetcode_test@10.6.5 / design-add-and-search-words-data-structure / index.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
27
28
29
30
31
32
33
34
35
import { PrefixTree } from "../implement-trie-prefix-tree/PrefixTree.ts";import { PrefixTreeInsert } from "./PrefixTreeInsert.ts";
interface WordDictionary { addWord(word: string): void; search(word: string): boolean;}export default WordDictionary;function WordDictionary(): WordDictionary { const root: PrefixTree = PrefixTree(); function insert(word: string): void { PrefixTreeInsert(root, word); } function search(word: string): boolean { return match(word, 0, root); }
return { addWord: insert, search };}
function match(word: string, index: number, root: PrefixTree): boolean { if (index === word.length) return root.isEnd;
if (word[index] !== ".") { const child = root.children.get(word[index]); if (!child) return false; return match(word, index + 1, child); } else { for (const child of root.children.values()) { if (match(word, index + 1, child)) return true; } } return false;}
masx200_leetcode_test

Version Info

Tagged at
a year ago