Problem Description
Design a data structure that supports inserting a string key with an integer value and returning the sum of all values for keys that share a given prefix. If a key is inserted again, its value should be updated to the new one.
Key Insights
- A brute-force solution iterates through all keys on every sum query, which is acceptable given the small constraints (max 50 calls).
- A Trie (prefix tree) data structure can optimize the sum query by aggregating prefix sums along the insertion path.
- When updating an existing key, the difference between the new value and the old value must be propagated through the Trie.
- Maintaining a hashmap for tracking the current value of each key simplifies handling duplicate key insertions.
Space and Time Complexity
Time Complexity:
- Insert: O(L) per key, where L is the length of the key.
- Sum: O(P) per query, where P is the length of the prefix. Space Complexity:
- O(N * L) in worst-case, where N is the number of keys and L is the average length of the keys.
Solution
We use a combination of a Trie and a HashMap. The HashMap will store the current value for each key to determine the delta when a key is updated. The Trie will store cumulative sums at each node such that each node represents a character in the keys. For insertion, we calculate the difference (delta) between the new value and the current value (if the key exists) and propagate this delta along the path of nodes in the Trie corresponding to the key. For sum query, we traverse the Trie using the prefix and return the stored cumulative sum at the node corresponding to the last character of the prefix. This approach efficiently handles prefix sum queries and dynamic updates.