A ledger maps string keys to integer values. Process q operations in order:
SET k v - set the value of key k to v, replacing any previous value of that key.SUM p - report the sum of the values of all keys that currently have p as a prefix (a key is a prefix of itself). If no key has that prefix, the sum is 0.Keys consist of lowercase English letters only; values are integers.
Line 1: an integer q.
Each of the next q lines: SET k v or SUM p.
For each SUM operation, in order, print a line with the requested total.
Example 1
Input
6 SET apple 3 SET app 2 SUM ap SUM app SET apple 10 SUM ap
Expected
5 5 12
Explanation
After the first two sets, apple=3 and app=2. SUM ap = 5 and SUM app = 5 (both keys start with app). After apple is reset to 10, SUM ap = 12.
Example 2
Input
3 SET cat 5 SUM ca SUM do
Expected
5 0
Explanation
cat starts with ca, so SUM ca = 5. No key starts with do, so SUM do = 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →