forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (35 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const Trie = require('../index');
const TrieNode = require('../Node');
function getAllUniqueWords(root, level, word) {
let result = [];
if (root.isEndOfWord) {
let temp = '';
for (let i = 0; i < level; i += 1) {
temp += String(word[i]);
}
result = [...result, temp];
}
for (let i = 0; i < 26; i += 1) {
if (root.children[i]) {
// eslint-disable-next-line no-param-reassign
word[level] = String.fromCharCode(i + 'a'.charCodeAt(0));
result = [...result, ...getAllUniqueWords(root.children[i], level + 1, word)];
}
}
return result;
}
function allUniqueWordsFromTrie(root) {
if (!(root instanceof TrieNode)) {
throw new Error('Invalid argument: Root of Trie is required');
}
const word = []; // char arr to store a word
for (let i = 0; i < 26; i += 1) {
word[i] = null;
}
return getAllUniqueWords(root, 0, word);
}
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
// const trie = new Trie();
// words.forEach(word => trie.insert(word));
// console.log(allUniqueWordsFromTrie(trie.root));
module.exports = allUniqueWordsFromTrie;