diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md index e0a0756216139..1274cc2d19475 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md @@ -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)); +}; +``` + diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md index 808ab3f98788c..e5cb5b9c96fc9 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md @@ -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)); +}; +``` + diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js new file mode 100644 index 0000000000000..944fd9f535995 --- /dev/null +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js @@ -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)); +};