Last active
October 31, 2016 14:09
-
-
Save fforres/a52aefc8ce55e4bcaee8f5d10f1cb040 to your computer and use it in GitHub Desktop.
hackerrank ctcy contacts - maps solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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