deno.land / x / masx200_leetcode_test@10.6.5 / longest-word-in-dictionary / 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
import { PrefixTreeInsert,} from "../design-add-and-search-words-data-structure/PrefixTreeInsert.ts";import { PrefixTree } from "../implement-trie-prefix-tree/PrefixTree.ts";import { PrefixTreeSearchEach } from "./PrefixTreeSearchEach.ts";
// import { TrieEach } from "./TrieEach.ts";
export default function longestWord(words: string[]): string { const root: PrefixTree = PrefixTree(); // const trie = TrieEach(); for (const word of words) { // trie.insert(word); PrefixTreeInsert(root, word); } let longest = ""; for (const word of words) { if (PrefixTreeSearchEach(root, word)) { if ( word.length > longest.length || (word.length === longest.length && word < longest) ) { longest = word; } } } return longest;}
masx200_leetcode_test

Version Info

Tagged at
a year ago