-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a21b1e8
Create Solution.js
07subhadip 324cc2b
Create Solution.js
07subhadip 80634a8
Update Solution.js
yanglbme 3680e8b
Delete solution/Solution.js
yanglbme 7a18121
Update README.md
yanglbme 46a2880
Update README_EN.md
yanglbme f4825db
Create Solution.js
07subhadip 8c24319
Update Solution.ts
07subhadip d6f4c63
Merge branch 'main' into main
yanglbme 9223b1f
style: format code and docs with prettier
yanglbme fa67038
Update Solution.ts
yanglbme cc15596
Update Solution.js
yanglbme 313f6b5
Update README.md
yanglbme 63ef10d
Update README_EN.md
yanglbme 6ae183c
Create Solution.js
07subhadip 5f7be59
Merge branch 'doocs:main' into main
07subhadip f77f588
style: format code and docs with prettier
07subhadip 1b9ab60
Update README.md
yanglbme 8b4d469
Update README_EN.md
yanglbme bf35864
Merge branch 'main' into main
acbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.