You are viewing a single comment's thread. Return to all comments →
This is my most pared down iterative version.
var Trie = function() { this.children = {}; this.count = 0; }; Trie.prototype.insert = function(str) { var iterator = this; for (var i = 0; i < str.length; i++) { var c = str.charAt(i); if (iterator.children[c] === undefined) { iterator.children[c] = new Trie(); } iterator.count++; iterator = iterator.children[c]; } iterator.count++; }; Trie.prototype.find = function(str) { var iterator = this; for (var i = 0; i < str.length; i++) { var c = str.charAt(i); if (iterator.children[c] === undefined) { return 0; } iterator = iterator.children[c]; } return iterator.count; };
Seems like cookies are disabled on this browser, please enable them to open this website
Tries: Contacts
You are viewing a single comment's thread. Return to all comments →
This is my most pared down iterative version.