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 524670e

Browse files
simplify backtracking algorithm
1 parent a0e0fd8 commit 524670e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

‎src/data-structures/trees/trie.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ class Trie {
6565
*
6666
* @param {string} prefix - The prefix to append to each word.
6767
* @param {string} node - Current node to start backtracking.
68-
* @param {string[]} words - Accumulated words.
69-
* @param {string} string - Current string.
7068
*/
71-
getAllWords(prefix = '', node = this, words = [], string = '') {
69+
getAllWords(prefix = '', node = this) {
70+
let words = [];
71+
7272
if (!node) { return words; }
7373
if (node.isWord) {
74-
words.push(`${prefix}${string}`);
74+
words.push(prefix);
7575
}
7676

7777
for (const char of Object.keys(node.children)) {
78-
this.getAllWords(prefix, node.children[char], words, `${string}${char}`);
78+
const newWords = this.getAllWords(`${prefix}${char}`, node.children[char]);
79+
words = words.concat(newWords);
7980
}
8081

8182
return words;

0 commit comments

Comments
(0)

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