Map Sum Pairs LeetCode solution in JavaScript
1var MapSum = function () {2this.hashKeys = new Map();3this.hashVals = new Map();4};56MapSum.prototype.insert = function (key, val) {7let prefix = "";8for (const str of key) {9prefix += str;10const hashKey = this.hashKeys.get(prefix) ?? new Set();11hashKey.add(key);12!this.hashKeys.has(prefix) && this.hashKeys.set(prefix, hashKey);13}14this.hashVals.set(key, val);15};1617MapSum.prototype.sum = function (prefix) {18const hashKey = this.hashKeys.get(prefix);19if (!hashKey) return 0;20let sum = 0;21hashKey.forEach((key) => (sum += this.hashVals.get(key)));22return sum;23};