304 North Cardinal St.
Dorchester Center, MA 02124

# Map Sum Pairs LeetCode Solution

## Problem – Map Sum Pairs

Design a map that allows you to do the following:

• Maps a string key to a given value.
• Returns the sum of the values that have a key with a prefix equal to a given string.

Implement the `MapSum` class:

• `MapSum()` Initializes the `MapSum` object.
• `void insert(String key, int val)` Inserts the `key-val` pair into the map. If the `key` already existed, the original `key-value` pair will be overridden to the new one.
• `int sum(string prefix)` Returns the sum of all the pairs’ value whose `key` starts with the `prefix`.

Example 1:

``````Input
["MapSum", "insert", "sum", "insert", "sum"]
[[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
Output
[null, null, 3, null, 5]

Explanation
MapSum mapSum = new MapSum();
mapSum.insert("apple", 3);
mapSum.sum("ap");           // return 3 (apple = 3)
mapSum.insert("app", 2);
mapSum.sum("ap");           // return 5 (apple + app = 3 + 2 = 5)
``````

Constraints:

• `1 <= key.length, prefix.length <= 50`
• `key` and `prefix` consist of only lowercase English letters.
• `1 <= val <= 1000`
• At most `50` calls will be made to `insert` and `sum`.

### Map Sum Pairs LeetCode Solution in C++

``````class MapSum {
public:
/** Initialize your data structure here. */
void insert(string key, int val) {
mp[key] = val;
}

int sum(string prefix) {
int sum = 0, n = prefix.size();
for (auto it = mp.lower_bound(prefix); it != mp.end() && it->first.substr(0, n) == prefix; it++)
sum += it->second;
return sum;
}
private:
map<string, int> mp;
};
``````

### Map Sum Pairs LeetCode Solution in Java

``````class MapSum {
class TrieNode {
Map<Character, TrieNode> children;
boolean isWord;
int value;

public TrieNode() {
children = new HashMap<Character, TrieNode>();
isWord = false;
value = 0;
}
}

TrieNode root;

/** Initialize your data structure here. */
public MapSum() {
root = new TrieNode();
}

public void insert(String key, int val) {
TrieNode curr = root;
for (char c : key.toCharArray()) {
TrieNode next = curr.children.get(c);
if (next == null) {
next = new TrieNode();
curr.children.put(c, next);
}
curr = next;
}
curr.isWord = true;
curr.value = val;
}

public int sum(String prefix) {
TrieNode curr = root;
for (char c : prefix.toCharArray()) {
TrieNode next = curr.children.get(c);
if (next == null) {
return 0;
}
curr = next;
}

return dfs(curr);
}

private int dfs(TrieNode root) {
int sum = 0;
for (char c : root.children.keySet()) {
sum += dfs(root.children.get(c));
}
return sum + root.value;
}
}
``````

### Map Sum Pairs LeetCode Solution in Python

``````class MapSum(object):

def __init__(self):
self.d = {}

def insert(self, key, val):
self.d[key] = val

def sum(self, prefix):
return sum(self.d[i] for i in self.d if i.startswith(prefix))
``````
##### Map Sum Pairs LeetCode Solution Review:

In our experience, we suggest you solve this Map Sum Pairs LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Map Sum Pairs LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Map Sum Pairs LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions