You are viewing a single comment's thread. Return to all comments →
I'm pretty happy with my functional version. It passes all of the tests.
const root = {} function main() { var n = parseInt(readLine()); for(var a0 = 0; a0 < n; a0++){ var op_temp = readLine().split(' '); var op = op_temp[0]; var contact = op_temp[1]; if (op === "add"){ add(contact, root) } if (op === "find"){ const result = find(contact, root) console.log(result) } } } const add = (value, node)=>{ const char = value.substring(0,1) if (node[char]){ node[char].count++ } else { node[char] = { count: 1} } if (value.length > 1){ const rest = value.substring(1) add(rest, node[char]) } } const find = (value, node)=>{ if (value.length == 1){ if (node[value]){ return node[value].count } return 0 } const char = value.substring(0,1) if (node[char]){ const rest = value.substring(1) return find(rest, node[char]) } return 0 }
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 →
I'm pretty happy with my functional version. It passes all of the tests.