File tree Expand file tree Collapse file tree 3 files changed +33
-4
lines changed
implement-trie-prefix-tree
sum-of-prefix-scores-of-strings Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/sum-of-prefix-scores-of-strings/
14
+
13
15
https://leetcode.cn/problems/count-days-spent-together/
14
16
15
17
https://leetcode.cn/problems/reverse-odd-levels-of-binary-tree/
Original file line number Diff line number Diff line change 1
- import { TrieNode } from "../implement-trie-ii-prefix-tree/TrieNode.ts" ;
2
- import { PrefixTree } from "./PrefixTree.ts" ;
3
-
4
- export function PrefixTreeSearchPrefix < T extends PrefixTree | TrieNode > (
1
+ export function PrefixTreeSearchPrefix < T extends { children : Map < string , T > } > (
5
2
root : T ,
6
3
prefix : string ,
7
4
{
Original file line number Diff line number Diff line change
1
+ import { TrieNode } from "../implement-trie-ii-prefix-tree/TrieNode.ts" ;
2
+ import { TrieNodeInsert } from "../implement-trie-ii-prefix-tree/TrieNodeInsert.ts" ;
3
+ import { PrefixTreeSearchPrefix } from "../implement-trie-prefix-tree/PrefixTreeSearchPrefix.ts" ;
4
+ function sumPrefixScores ( words : string [ ] ) : number [ ] {
5
+ const root = new TrieNode ( ) ;
6
+ for ( const word of words ) {
7
+ TrieNodeInsert ( root , word ) ;
8
+ }
9
+ const word2sum = new Map < string , number > ( ) ;
10
+ return words . map ( ( word ) => {
11
+ const sum = word2sum . get ( word ) ?? TrieNodeSum ( root , word ) ;
12
+ word2sum . set ( word , sum ) ;
13
+ return sum ;
14
+ } ) ;
15
+ }
16
+ export default sumPrefixScores ;
17
+
18
+ function TrieNodeSum ( root : TrieNode , word : string ) : number {
19
+ let sum = 0 ;
20
+ let index = 0 ;
21
+ PrefixTreeSearchPrefix ( root , word , {
22
+ each ( node ) {
23
+ if ( index ) {
24
+ sum += node . prefixCount ;
25
+ }
26
+ index ++ ;
27
+ } ,
28
+ } ) ;
29
+ return sum ;
30
+ }
You can’t perform that action at this time.
0 commit comments