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

feat: add js solution to lc problem: No.2416 #3560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
acbin merged 20 commits into doocs:main from 07subhadip:main
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a21b1e8
Create Solution.js
07subhadip Sep 23, 2024
324cc2b
Create Solution.js
07subhadip Sep 23, 2024
80634a8
Update Solution.js
yanglbme Sep 24, 2024
3680e8b
Delete solution/Solution.js
yanglbme Sep 24, 2024
7a18121
Update README.md
yanglbme Sep 24, 2024
46a2880
Update README_EN.md
yanglbme Sep 24, 2024
f4825db
Create Solution.js
07subhadip Sep 24, 2024
8c24319
Update Solution.ts
07subhadip Sep 24, 2024
d6f4c63
Merge branch 'main' into main
yanglbme Sep 24, 2024
9223b1f
style: format code and docs with prettier
yanglbme Sep 24, 2024
fa67038
Update Solution.ts
yanglbme Sep 24, 2024
cc15596
Update Solution.js
yanglbme Sep 24, 2024
313f6b5
Update README.md
yanglbme Sep 24, 2024
63ef10d
Update README_EN.md
yanglbme Sep 24, 2024
6ae183c
Create Solution.js
07subhadip Sep 25, 2024
5f7be59
Merge branch 'doocs:main' into main
07subhadip Sep 25, 2024
f77f588
style: format code and docs with prettier
07subhadip Sep 25, 2024
1b9ab60
Update README.md
yanglbme Sep 25, 2024
8b4d469
Update README_EN.md
yanglbme Sep 25, 2024
bf35864
Merge branch 'main' into main
acbin Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,53 @@ function sumPrefixScores(words: string[]): number[] {
}
```

#### JavaScript

```js
class Trie {
constructor() {
this.children = {};
this.cnt = 0;
}

insert(w) {
let node = this;
for (const c of w) {
if (!node.children[c]) {
node.children[c] = new Trie();
}
node = node.children[c];
node.cnt++;
}
}

search(w) {
let node = this;
let ans = 0;
for (const c of w) {
if (!node.children[c]) {
return ans;
}
node = node.children[c];
ans += node.cnt;
}
return ans;
}
}

/**
* @param {string[]} words
* @return {number[]}
*/
var sumPrefixScores = function (words) {
const trie = new Trie();
for (const w of words) {
trie.insert(w);
}
return words.map(w => trie.search(w));
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,53 @@ function sumPrefixScores(words: string[]): number[] {
}
```

#### JavaScript

```js
class Trie {
constructor() {
this.children = {};
this.cnt = 0;
}

insert(w) {
let node = this;
for (const c of w) {
if (!node.children[c]) {
node.children[c] = new Trie();
}
node = node.children[c];
node.cnt++;
}
}

search(w) {
let node = this;
let ans = 0;
for (const c of w) {
if (!node.children[c]) {
return ans;
}
node = node.children[c];
ans += node.cnt;
}
return ans;
}
}

/**
* @param {string[]} words
* @return {number[]}
*/
var sumPrefixScores = function (words) {
const trie = new Trie();
for (const w of words) {
trie.insert(w);
}
return words.map(w => trie.search(w));
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Trie {
constructor() {
this.children = {};
this.cnt = 0;
}

insert(w) {
let node = this;
for (const c of w) {
if (!node.children[c]) {
node.children[c] = new Trie();
}
node = node.children[c];
node.cnt++;
}
}

search(w) {
let node = this;
let ans = 0;
for (const c of w) {
if (!node.children[c]) {
return ans;
}
node = node.children[c];
ans += node.cnt;
}
return ans;
}
}

/**
* @param {string[]} words
* @return {number[]}
*/
var sumPrefixScores = function (words) {
const trie = new Trie();
for (const w of words) {
trie.insert(w);
}
return words.map(w => trie.search(w));
};
Loading

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