Skip to content

Commit d73e6d9

Browse files
authored
Update 2416-sum-of-prefix-scores-of-strings.js
1 parent 2b479b7 commit d73e6d9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

2416-sum-of-prefix-scores-of-strings.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
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+
145
/**
246
* @param {string[]} words
347
* @return {number[]}

0 commit comments

Comments
 (0)