Skip to content

Instantly share code, notes, and snippets.

@fforres
Last active October 31, 2016 14:09
Show Gist options
  • Save fforres/a52aefc8ce55e4bcaee8f5d10f1cb040 to your computer and use it in GitHub Desktop.
Save fforres/a52aefc8ce55e4bcaee8f5d10f1cb040 to your computer and use it in GitHub Desktop.
hackerrank ctcy contacts - maps solution
https://www.hackerrank.com/challenges/ctci-contacts
/////////////// ignore above this line ////////////////////
function analizeContacts(){
this.contactObject = new Map();
this.currentFinds = [];
}
analizeContacts.prototype.find = function(term){
let currentWords = null;
if(this.contactObject.get(term)) {
currentWords = this.contactObject.get(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);
let amount = this.contactObject.get(el) ? this.contactObject.get(el) + 1 : 1 ;
this.contactObject.set(el, amount);
}
}
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