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 392cd98

Browse files
Olegtrekhleb
Oleg
authored andcommitted
Fix method Trie::doesWordExist() (trekhleb#175)
Method Trie::doesWordExist() return `true` when word is complete otherwise `false`
1 parent 5eb1195 commit 392cd98

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export default class Trie {
4343
* @return {boolean}
4444
*/
4545
doesWordExist(word) {
46-
return !!this.getLastCharacterNode(word);
46+
const lastCharacter = this.getLastCharacterNode(word);
47+
48+
return !!lastCharacter && lastCharacter.isCompleteWord;
4749
}
4850

4951
/**

‎src/data-structures/trie/__test__/Trie.test.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ describe('Trie', () => {
4545
trie.addWord('caption');
4646

4747
expect(trie.doesWordExist('cat')).toBe(true);
48-
expect(trie.doesWordExist('cap')).toBe(true);
48+
expect(trie.doesWordExist('cats')).toBe(true);
49+
expect(trie.doesWordExist('cap')).toBe(false);
4950
expect(trie.doesWordExist('call')).toBe(false);
5051
});
5152
});

0 commit comments

Comments
(0)

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