You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Initialize your data structure here. */varTrieNode=function(){this.links=newMap();}varTrie=function(){this.root=newTrieNode();this.root.links.set(0,true);};/** * Inserts a word into the trie. * @param {string} word * @return {void} */Trie.prototype.insert=function(word){varnode=this.root;for(varwofword){if(node.links.has(w)){node=node.links.get(w);}else{node.links.set(w,newTrieNode());node=node.links.get(w);}}node.links.set(0,true);};/** * Returns if the word is in the trie. * @param {string} word * @return {boolean} */Trie.prototype.search=function(word){varnode=this.root;for(varwofword){if(!node.links.has(w)){returnfalse;}node=node.links.get(w);}returnnode.links.has(0);};/** * Returns if there is any word in the trie that starts with the given prefix. * @param {string} prefix * @return {boolean} */Trie.prototype.startsWith=function(prefix){varnode=this.root;for(varwofprefix){if(!node.links.has(w)){returnfalse;}node=node.links.get(w);}returntrue;};
The text was updated successfully, but these errors were encountered:
习题
思路
用map来保存当前字母后续的字母,并用一个变量来保存当前字母是否为一个单词的末尾
解答
JavaScript
The text was updated successfully, but these errors were encountered: