Map Sum Pairs
Design a MapSum structure supporting insert(key, val), which stores a string key with an integer value (overwriting a previous value for the same key), and sum(prefix), which returns the total of the values of all keys that start with the given prefix.
Open official problem prompt ↗Support fast prefix-sum queries over a growing set of key-value pairs, with correct overwrites.
Like a filing cabinet where every drawer front shows the running total of all folders filed deeper inside it, updated as you file each folder.
- Input
- insert("apple", 3); sum("ap"); insert("app", 2); sum("ap")
- Output
- [null, 3, null, 5]
- Why
- After inserting apple=3, only 'apple' starts with 'ap' (sum 3); after inserting app=2, both 'apple' and 'app' start with 'ap' (sum 5).
1 <= key.length, prefix.length <= 50keys and prefixes consist of lowercase English letters1 <= val <= 1000At most 50 calls to insert and sum