Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 46c5add

Browse files
feat: add js solution to lc problem: No.2416 (doocs#3560)
1 parent 7d7863a commit 46c5add

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

‎solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,53 @@ function sumPrefixScores(words: string[]): number[] {
337337
}
338338
```
339339

340+
#### JavaScript
341+
342+
```js
343+
class Trie {
344+
constructor() {
345+
this.children = {};
346+
this.cnt = 0;
347+
}
348+
349+
insert(w) {
350+
let node = this;
351+
for (const c of w) {
352+
if (!node.children[c]) {
353+
node.children[c] = new Trie();
354+
}
355+
node = node.children[c];
356+
node.cnt++;
357+
}
358+
}
359+
360+
search(w) {
361+
let node = this;
362+
let ans = 0;
363+
for (const c of w) {
364+
if (!node.children[c]) {
365+
return ans;
366+
}
367+
node = node.children[c];
368+
ans += node.cnt;
369+
}
370+
return ans;
371+
}
372+
}
373+
374+
/**
375+
* @param {string[]} words
376+
* @return {number[]}
377+
*/
378+
var sumPrefixScores = function (words) {
379+
const trie = new Trie();
380+
for (const w of words) {
381+
trie.insert(w);
382+
}
383+
return words.map(w => trie.search(w));
384+
};
385+
```
386+
340387
<!-- tabs:end -->
341388

342389
<!-- solution:end -->

‎solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,53 @@ function sumPrefixScores(words: string[]): number[] {
337337
}
338338
```
339339

340+
#### JavaScript
341+
342+
```js
343+
class Trie {
344+
constructor() {
345+
this.children = {};
346+
this.cnt = 0;
347+
}
348+
349+
insert(w) {
350+
let node = this;
351+
for (const c of w) {
352+
if (!node.children[c]) {
353+
node.children[c] = new Trie();
354+
}
355+
node = node.children[c];
356+
node.cnt++;
357+
}
358+
}
359+
360+
search(w) {
361+
let node = this;
362+
let ans = 0;
363+
for (const c of w) {
364+
if (!node.children[c]) {
365+
return ans;
366+
}
367+
node = node.children[c];
368+
ans += node.cnt;
369+
}
370+
return ans;
371+
}
372+
}
373+
374+
/**
375+
* @param {string[]} words
376+
* @return {number[]}
377+
*/
378+
var sumPrefixScores = function (words) {
379+
const trie = new Trie();
380+
for (const w of words) {
381+
trie.insert(w);
382+
}
383+
return words.map(w => trie.search(w));
384+
};
385+
```
386+
340387
<!-- tabs:end -->
341388

342389
<!-- solution:end -->
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Trie {
2+
constructor() {
3+
this.children = {};
4+
this.cnt = 0;
5+
}
6+
7+
insert(w) {
8+
let node = this;
9+
for (const c of w) {
10+
if (!node.children[c]) {
11+
node.children[c] = new Trie();
12+
}
13+
node = node.children[c];
14+
node.cnt++;
15+
}
16+
}
17+
18+
search(w) {
19+
let node = this;
20+
let ans = 0;
21+
for (const c of w) {
22+
if (!node.children[c]) {
23+
return ans;
24+
}
25+
node = node.children[c];
26+
ans += node.cnt;
27+
}
28+
return ans;
29+
}
30+
}
31+
32+
/**
33+
* @param {string[]} words
34+
* @return {number[]}
35+
*/
36+
var sumPrefixScores = function (words) {
37+
const trie = new Trie();
38+
for (const w of words) {
39+
trie.insert(w);
40+
}
41+
return words.map(w => trie.search(w));
42+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /