File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } words
3
+ * @return {number[] }
4
+ */
5
+ const sumPrefixScores = function ( words ) {
6
+ const root = new Node ( )
7
+ const n = words . length
8
+ for ( const w of words ) {
9
+ let cur = root
10
+ for ( const ch of w ) {
11
+ if ( ! cur . children . has ( ch ) ) cur . children . set ( ch , new Node ( ) )
12
+ const node = cur . children . get ( ch )
13
+ node . cnt ++
14
+ cur = node
15
+ }
16
+ }
17
+
18
+ const res = [ ]
19
+
20
+ for ( const w of words ) {
21
+ let cur = root
22
+ let tmp = 0
23
+ for ( const ch of w ) {
24
+ if ( cur . children . has ( ch ) ) {
25
+ const node = cur . children . get ( ch )
26
+ tmp += node . cnt
27
+ cur = node
28
+ } else break
29
+ }
30
+ res . push ( tmp )
31
+ }
32
+
33
+ return res
34
+ } ;
35
+
36
+ class Node {
37
+ constructor ( ) {
38
+ this . children = new Map ( )
39
+ this . cnt = 0
40
+ }
41
+ }
42
+
43
+ // another
44
+
1
45
/**
2
46
* @param {string[] } words
3
47
* @return {number[] }
You can’t perform that action at this time.
0 commit comments