diff --git a/src/data-structures/trie/Trie.js b/src/data-structures/trie/Trie.js index 8eff5a05..39f25820 100644 --- a/src/data-structures/trie/Trie.js +++ b/src/data-structures/trie/Trie.js @@ -43,7 +43,9 @@ export default class Trie { * @return {boolean} */ doesWordExist(word) { - return !!this.getLastCharacterNode(word); + const lastCharacter = this.getLastCharacterNode(word); + + return !!lastCharacter && lastCharacter.isCompleteWord; } /** diff --git a/src/data-structures/trie/__test__/Trie.test.js b/src/data-structures/trie/__test__/Trie.test.js index 75867143..b66db94a 100644 --- a/src/data-structures/trie/__test__/Trie.test.js +++ b/src/data-structures/trie/__test__/Trie.test.js @@ -45,7 +45,8 @@ describe('Trie', () => { trie.addWord('caption'); expect(trie.doesWordExist('cat')).toBe(true); - expect(trie.doesWordExist('cap')).toBe(true); + expect(trie.doesWordExist('cats')).toBe(true); + expect(trie.doesWordExist('cap')).toBe(false); expect(trie.doesWordExist('call')).toBe(false); }); });