`{"id":"65e01308-77b3-4aba-9ad8-60c4af563776","name":"Map Sum Pairs","description":"Implement the MapSum class:\r\n1. MapSum() Initializes the MapSum object.\r\n2. 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.\r\n3. int sum(string prefix) Returns the sum of all the pairs' value whose key starts with the prefix.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= key.length, prefix.length &lt;= 50\r\n2. key and prefix consist of only lowercase English letters.\r\n3. 1 &lt;= val &lt;= 1000","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h> \n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid insert(string key , int val){\n // write your code here\n \n}\n\nint sum(string prefix){\n // write your code here\n}\n\n\n\n\n\n\nint main() {\n string ss;\n root = new Node();\n while (getline(cin, ss)) {\n string check = ss.substr(0, 4);\n if (check == \"exit\") break;\n vector<string> arr;\n int i = 0;\n string temp = \"\";\n for(int i=0; i<ss.size(); i++) {\n if (ss[i] == ' ') {\n arr.push_back(temp);\n temp = \"\";\n }\n else {\n temp.push_back(ss[i]);\n }\n }\n if(temp != \"\"){\n arr.push_back(temp);\n }\n\n\n\n if (arr.size() == 2) {\n insert(arr, stoi(arr.substr(0, arr.size() - 1)));\n }\n else {\n cout << sum(arr.substr(0, arr.size() - 1)) << endl;\n }\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MapSum {\r\n\r\n /** Initialize your data structure here. */\r\n public MapSum() {\r\n\r\n }\r\n\r\n public void insert(String key, int val) {\r\n\r\n }\r\n\r\n public int sum(String prefix) {\r\n\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n MapSum obj = new MapSum();\r\n\r\n while (read.ready()) {\r\n String inp[] = read.readLine().split(\" \");\r\n if (inp.equals(\"exit\"))break;\r\n\r\n if (inp.length == 2) {\r\n obj.insert(inp, Integer.parseInt(inp));\r\n } else {\r\n System.out.println(obj.sum(inp));\r\n }\r\n }\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"apple 3\r\nap\r\napp 2\r\napple 2\r\nap\r\nexit\r\n","sampleOutput":"3\r\n4\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"4b4725b5-4896-4b68-aae2-03ea1e2ecbb2","name":"Trie For Intermediate","slug":"trie-for-intermediate-9996","type":0},{"id":"5cbe857c-e52c-4341-96a4-7801d8569c8a","name":"Map Sum Pairs MCQ","slug":"map-sum-pairs-mcq","type":0},{"id":"47ff3028-443a-48f9-8978-d40b39cbf3f9","name":"Map Sum Pairs","slug":"map-sum-pairs","type":1}],"next":null,"prev":null}}`

# Map Sum Pairs

Implement the MapSum class: 1. MapSum() Initializes the MapSum object. 2. 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. 3. int sum(string prefix) Returns the sum of all the pairs' value whose key starts with the prefix.

`{"id":"65e01308-77b3-4aba-9ad8-60c4af563776","name":"Map Sum Pairs","description":"Implement the MapSum class:\r\n1. MapSum() Initializes the MapSum object.\r\n2. 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.\r\n3. int sum(string prefix) Returns the sum of all the pairs' value whose key starts with the prefix.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= key.length, prefix.length &lt;= 50\r\n2. key and prefix consist of only lowercase English letters.\r\n3. 1 &lt;= val &lt;= 1000","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h> \n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nvoid insert(string key , int val){\n // write your code here\n \n}\n\nint sum(string prefix){\n // write your code here\n}\n\n\n\n\n\n\nint main() {\n string ss;\n root = new Node();\n while (getline(cin, ss)) {\n string check = ss.substr(0, 4);\n if (check == \"exit\") break;\n vector<string> arr;\n int i = 0;\n string temp = \"\";\n for(int i=0; i<ss.size(); i++) {\n if (ss[i] == ' ') {\n arr.push_back(temp);\n temp = \"\";\n }\n else {\n temp.push_back(ss[i]);\n }\n }\n if(temp != \"\"){\n arr.push_back(temp);\n }\n\n\n\n if (arr.size() == 2) {\n insert(arr, stoi(arr.substr(0, arr.size() - 1)));\n }\n else {\n cout << sum(arr.substr(0, arr.size() - 1)) << endl;\n }\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MapSum {\r\n\r\n /** Initialize your data structure here. */\r\n public MapSum() {\r\n\r\n }\r\n\r\n public void insert(String key, int val) {\r\n\r\n }\r\n\r\n public int sum(String prefix) {\r\n\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n MapSum obj = new MapSum();\r\n\r\n while (read.ready()) {\r\n String inp[] = read.readLine().split(\" \");\r\n if (inp.equals(\"exit\"))break;\r\n\r\n if (inp.length == 2) {\r\n obj.insert(inp, Integer.parseInt(inp));\r\n } else {\r\n System.out.println(obj.sum(inp));\r\n }\r\n }\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"apple 3\r\nap\r\napp 2\r\napple 2\r\nap\r\nexit\r\n","sampleOutput":"3\r\n4\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"4b4725b5-4896-4b68-aae2-03ea1e2ecbb2","name":"Trie For Intermediate","slug":"trie-for-intermediate-9996","type":0},{"id":"5cbe857c-e52c-4341-96a4-7801d8569c8a","name":"Map Sum Pairs MCQ","slug":"map-sum-pairs-mcq","type":0},{"id":"47ff3028-443a-48f9-8978-d40b39cbf3f9","name":"Map Sum Pairs","slug":"map-sum-pairs","type":1}],"next":null,"prev":null}}` Editor

# Map Sum Pairs

easy

Implement the MapSum class: 1. MapSum() Initializes the MapSum object. 2. 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. 3. int sum(string prefix) Returns the sum of all the pairs' value whose key starts with the prefix.

## Constraints

1. 1 <= key.length, prefix.length <= 50 2. key and prefix consist of only lowercase English letters. 3. 1 <= val <= 1000

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}apple 3 ap app 2 apple 2 ap exit ```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3 4 ```

Discussions

Show Discussion

Related Resources 