|
| 1 | +export {} |
| 2 | +class TrieNode { |
| 3 | + char: string |
| 4 | + count: number = 0 |
| 5 | + countPrefix: number = 0 |
| 6 | + children: { [key: string]: TrieNode | undefined } = {} |
| 7 | + constructor (char: string) { |
| 8 | + this.char = char |
| 9 | + } |
| 10 | +}; |
| 11 | +class Trie { |
| 12 | + trie: TrieNode |
| 13 | + /** |
| 14 | + * @description Initialize the trie |
| 15 | + */ |
| 16 | + constructor () { |
| 17 | + this.trie = new TrieNode('/') |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @description Insert strings into the trie |
| 22 | + * @param str the string to be inserted |
| 23 | + * @param count number of `str` to be inserted, can be negative for deleting x from the trie |
| 24 | + */ |
| 25 | + insert (str: string, count: number = 1): void { |
| 26 | + let cur = this.trie |
| 27 | + for (const char of str) { |
| 28 | + cur.children[char] ??= new TrieNode(char) |
| 29 | + cur = cur.children[char]! |
| 30 | + cur.countPrefix += count |
| 31 | + } |
| 32 | + cur.count += count |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Traverse the trie with a callback function |
| 37 | + * @param str An input string |
| 38 | + * @param callbackfn A callback function. traverse calls the callbackfn function one time for each char in `str`, however it may skip ending chars in str if the trie has no children to go deeper, the returned char from callbackfn will be used as the direction of traversing |
| 39 | + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value |
| 40 | + */ |
| 41 | + traverse (str: string, callbackfn: (char: string, idx: number, node: TrieNode) => string, thisArg?: any): void { |
| 42 | + let cur = this.trie |
| 43 | + for (let i = 0; i < str.length; i++) { |
| 44 | + const retChar = callbackfn.call(thisArg, str[i], i, cur) |
| 45 | + const tmp = cur.children[retChar] |
| 46 | + if (!tmp || tmp.countPrefix <= 0) return |
| 47 | + cur = tmp |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @description Search a string in the trie |
| 53 | + * @returns Number of `str` in the trie |
| 54 | + */ |
| 55 | + count (str: string): number { |
| 56 | + let ans = 0 |
| 57 | + this.traverse(str, (char, idx, node) => { |
| 58 | + const nextNode = node.children[char] |
| 59 | + if (idx === str.length - 1 && nextNode) { |
| 60 | + ans = nextNode.count |
| 61 | + } |
| 62 | + return char |
| 63 | + }) |
| 64 | + return ans |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @description Search a string in the trie |
| 69 | + * @returns Number of prefix of `str` in the trie |
| 70 | + */ |
| 71 | + countPrefix (str: string): number { |
| 72 | + let ans = 0 |
| 73 | + this.traverse(str, (char, idx, node) => { |
| 74 | + const nextNode = node.children[char] |
| 75 | + ans += nextNode?.countPrefix ?? 0 |
| 76 | + return char |
| 77 | + }) |
| 78 | + return ans |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +function sumPrefixScores (words: string[]): number[] { |
| 83 | + const trie = new Trie() |
| 84 | + const ans = [] |
| 85 | + for (const w of words) { |
| 86 | + trie.insert(w, 1) |
| 87 | + } |
| 88 | + for (const w of words) { |
| 89 | + ans.push(trie.countPrefix(w)) |
| 90 | + } |
| 91 | + return ans |
| 92 | +}; |
0 commit comments