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 f0ca6ed

Browse files
committed
update: unique count
1 parent a6b3ce7 commit f0ca6ed

File tree

2 files changed

+25
-2
lines changed
  • src/_DataStructures_/Trees/Trie

2 files changed

+25
-2
lines changed

‎src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Trie = require('../index');
44
function totalWords(root) {
55
let result = 0;
66
if (root.isEndOfWord) {
7-
result += 1;
7+
result += root.wordCount;
88
}
99
for (let i = 0; i < 26; i += 1) {
1010
if (root.children[i] !== null) {
@@ -14,7 +14,7 @@ function totalWords(root) {
1414
return result;
1515
}
1616

17-
// const words = ['bed', 'ball', 'apple', 'java', 'javascript'];
17+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
1818
// const trie = new Trie();
1919

2020
// words.forEach(word => trie.insert(word));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable no-unused-vars */
2+
const Trie = require('../index');
3+
4+
function uniqueWordCount(root) {
5+
let result = 0;
6+
if (root.isEndOfWord) {
7+
result += 1;
8+
}
9+
for (let i = 0; i < 26; i += 1) {
10+
if (root.children[i]) {
11+
result += uniqueWordCount(root.children[i]);
12+
}
13+
}
14+
return result;
15+
}
16+
17+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
18+
// const trie = new Trie();
19+
20+
// words.forEach(word => trie.insert(word));
21+
// console.log(uniqueWordCount(trie.root));
22+
23+
module.exports = uniqueWordCount;

0 commit comments

Comments
(0)

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