-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact2.js
156 lines (121 loc) · 2.9 KB
/
contact2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
// https://www.hackerrank.com/challenges/contacts/problem
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
const value = ''
/* console.log(JSON.stringify(value).length)
const typeSizes = {
"undefined": () => 0,
"boolean": () => 8,
"number": () => 8,
"string": item => 2 * item.length,
"object": item => !item ? 0 : Object
.keys(item)
.reduce((total, key) => sizeOf(key) + sizeOf(item[key]) + total, 0)
};
const type = typeof value
const sizeOf = value => typeSizes[type](value)
console.log(sizeOf(value))
return */
/*
* Complete the contacts function below.
*/
function Node(data) {
this.data = data
this.isEndOfWord = false
this.children = {}
}
function Trie() {
this.root = new Node()
}
Trie.prototype.search = function () {
}
Trie.prototype.insert = function (word) {
let node = this.root
for (let char of word) {
if (node[char] == null) {
node[char] = {}
}
node = node[char]
}
node.isEndOfWord = true
}
const cache = new Map()
let allContacts = " "
function setCache(key, value) {
// console.log(`>>> set cache ${key}: ${value}`)
cache.set(key, value)
}
function getCache(key) {
let value = null
if (cache.get(key)) {
return cache.get(key)
}
for (var k of cache.keys()) {
// console.log(k)
if (k.indexOf(key) === 0) {
value = cache.get(k)
break
}
}
return value
}
function add(value) {
allContacts = allContacts + " " + value
}
function search(value) {
if (allContacts === ' ') {
return 0
}
let sum = getCache(value)
if (sum === null) {
// there is no cache
if (value.length === 1) {
sum = (allContacts.match(new RegExp("\\b"+value+"(\\w*)?", 'g')) || []).length
} else {
sum = allContacts.split(` ${value}`).length -1
}
//set cache
setCache(value, sum)
}
return sum
}
function contacts(queries) {
let results = []
for (let x = 0; x < queries.length; x++) {
const command = '' + queries[x][0]
const value = '' + queries[x][1]
if (command === 'add') {
add(value)
} else if (command === 'find') {
// console.log('find', value)
let sum = search(value)
results.push(sum)
console.log(sum)
}
}
console.log(allContacts)
// console.log('cache', cache)
return results
}
//////
/**
* nui1
* nui2
* nui
// se chave do map comecar com value procurado
// se o valor procurado.lenght < que chave do Map atual.length
// entao soma é igual a soma de todos os values de todas as chaves que comecam com o valor procurado
*/
let reader = fs.createReadStream('input02.txt');
reader.on('data', function (chunk) {
inputString += chunk.toString()
});
reader.on("end", () => {
inputString = inputString.trim().split('\n').map(str => str.trim().split(" "))
inputString.shift()
const result = contacts(inputString)
process.exit(0)
});