All problems

Map Sum Pairs

Medium · trie

A table maps text keys to whole numbers. It starts empty and receives a list of operations. For each position i, operations[i] is either "insert" or "sum", keys[i] is a lowercase word, and values[i] is a number.

Return a list with the answer to every "sum" operation, in the order they appear.

Examples

Example 1

Input: operations = ["insert", "sum", "insert", "sum", "insert", "sum"], keys = ["apple", "ap", "app", "ap", "apple", "ap"], values = [3, 0, 2, 0, 5, 0]

Output: [3, 5, 7]

After the first insert only "apple" (3) starts with "ap". Adding "app" (2) makes the total 5. Inserting "apple" again replaces 3 with 5, so the total is 5 + 2 = 7.

Example 2

Input: operations = ["insert", "insert", "sum", "sum"], keys = ["cat", "car", "ca", "dog"], values = [4, -1, 0, 0]

Output: [3, 0]

Both keys start with "ca", so the first answer is 4 + (-1) = 3. No key starts with "dog".

Constraints