Map Sum Pairs Problem

Map Sum Pairs Problem — ExecCode Medium DSA Practice

Solve the Map Sum Pairs problem on ExecCode. Free online medium DSA practice in Design. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Design a map that supports inserting a (key, value) pair and computing the sum of values of all keys that share a given prefix. insert(key, val) sets the value for key (overwriting any earlier value for the same key), and sum(prefix) returns the total value across every currently stored key that begins with prefix.

Examples

Input {"ops": ["insert", "sum", "insert", "sum"], "vals": ["apple:3", "ap", "append:2", "ap"]}; Output [None, 3, None, 5]. Input {"ops": ["insert", "sum"], "vals": ["a:3", "a"]}; Output [None, 3]. Input {"ops": ["insert", "insert", "insert", "sum", "sum"], "vals": ["ab:2", "abc:3", "ac:1", "a", "ab"]}; Output [None, None, None, 6, 5]

Constraints

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

Practice Map Sum Pairs free on ExecCode. Browse DSA problems, topic map, and placement guides.