Skip to content

Instantly share code, notes, and snippets.

@fforres
Created October 31, 2016 14:09
Show Gist options
  • Save fforres/f08e21a4e2c26e73d77bfb9daf4c7e65 to your computer and use it in GitHub Desktop.
Save fforres/f08e21a4e2c26e73d77bfb9daf4c7e65 to your computer and use it in GitHub Desktop.
hackerrank ctcy contacts - object solution
https://www.hackerrank.com/challenges/ctci-contacts
/////////////// ignore above this line ////////////////////
function analizeContacts(){
this.contactObject = {};
this.currentFinds = [];
}
analizeContacts.prototype.find = function(term){
let currentWords = null;
if(this.contactObject[term]) {
currentWords = this.contactObject[term];
} else {
currentWords = 0;
}
this.currentFinds.push(currentWords);
}
analizeContacts.prototype.add = function(term) {
for( let i = 1; i < term.length + 1; i++) {
const el = term.substring(0, i);
this.contactObject[el] = this.contactObject[el] ? this.contactObject[el] + 1 : 1 ;
}
}
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