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
classTrieNode{constructor(){this.children={};this.isEndOfWord=false;}}classTrie{constructor(){this.root=newTrieNode();}insert(word){this.traverseAndInsert(this.root.children,word);}traverseAndInsert(node,word){letwordLength=word.length;letallCharacters=[...word];// if the sequence already exists don't create oneif(!node[allCharacters[0]]){node[allCharacters[0]]=newTrieNode();}// if it is the last character then mark it as endif(wordLength===1){return(node[allCharacters[0]].isEndOfWord=true);}// if it is not the last character, keep on traversingif(wordLength>0){this.traverseAndInsert(node[allCharacters[0]].children,allCharacters.slice(1).join(""));}}}consttrie=newTrie();trie.insert("car");trie.insert("cat");trie.insert("ok");
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Beta Was this translation helpful? Give feedback.
All reactions