Skip to content

Instantly share code, notes, and snippets.

@fforres
Last active October 31, 2016 14:09
Show Gist options
  • Save fforres/f159abd18f095827293f7509bec53e65 to your computer and use it in GitHub Desktop.
Save fforres/f159abd18f095827293f7509bec53e65 to your computer and use it in GitHub Desktop.
hackerrank ctcy contacts - fisrt solution
https://www.hackerrank.com/challenges/ctci-contacts
/////////////// ignore above this line ////////////////////
function analizeContacts(){
this.contactObject = {
letters: {},
data: 0
};
this.currentFinds = [];
}
analizeContacts.prototype.find = function(term){
const termChars = term.split('');
let currentWords = null;
let searchObject = this.contactObject.letters;
for( let i = 0; i < termChars.length; i++) {
const el = termChars[i];
if(searchObject[el]) {
currentWords = searchObject[el].data;
searchObject = searchObject[el].letters;
} else {
currentWords = 0;
break;
}
}
if(currentWords !== null) {
this.currentFinds.push(currentWords);
}
}
analizeContacts.prototype.add = function(term) {
const termChars = term.split('');
let searchObject = this.contactObject;
for( let i = 0; i < termChars.length; i++) {
const el = termChars[i];
if (!searchObject.letters[el]) {
searchObject.letters[el] = {};
searchObject.letters[el].letters = {};
searchObject.letters[el].data = 1;
} else {
searchObject.letters[el].data++
}
searchObject = searchObject.letters[el];
}
}
analizeContacts.prototype.results = function() {
this.currentFinds.forEach((el) => {
console.log(el);
});
}
function main() {
var n = parseInt(readLine());
const analize = new analizeContacts();
for(var a0 = 0; a0 < n; a0++){
var op_temp = readLine().split(' ');
var op = op_temp[0];
var contact = op_temp[1];
analize[op](contact);
}
analize.results()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment