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 3c3d2c4

Browse files
committed
update: find all unique words
1 parent 2fbc809 commit 3c3d2c4

File tree

1 file changed

+40
-0
lines changed
  • src/_DataStructures_/Trees/Trie/get-unique-words

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const Trie = require('../index');
2+
3+
function getAllUniqueWords(root, level, word) {
4+
let result = [];
5+
6+
if (!root) return result;
7+
8+
if (root.isEndOfWord) {
9+
let temp = '';
10+
for (let i = 0; i < level; i += 1) {
11+
temp += String(word[i]);
12+
}
13+
result = [...result, temp];
14+
}
15+
16+
for (let i = 0; i < 26; i += 1) {
17+
if (root.children[i]) {
18+
// eslint-disable-next-line no-param-reassign
19+
word[level] = String.fromCharCode(i + 'a'.charCodeAt(0));
20+
result = [...result, ...getAllUniqueWords(root.children[i], level + 1, word)];
21+
}
22+
}
23+
return result;
24+
}
25+
26+
function allUniqueWordsFromTrie(root) {
27+
const word = []; // char arr to store a word
28+
for (let i = 0; i < 26; i += 1) {
29+
word[i] = null;
30+
}
31+
return getAllUniqueWords(root, 0, word);
32+
}
33+
34+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
35+
// const trie = new Trie();
36+
37+
// words.forEach(word => trie.insert(word));
38+
// console.log(allUniqueWordsFromTrie(trie.root));
39+
40+
module.exports = allUniqueWordsFromTrie;

0 commit comments

Comments
(0)

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