Merge and Filter Prioritized Configurations
Problem statement
You are given configuration tables and their integer priorities. Each table contains strings intended to have the form key=value, where a valid value is a signed 32-bit integer.
Merge the tables, then return only the requested keys.
Merge rules
- A valid entry has an ASCII identifier key matching
[A-Za-z_][A-Za-z0-9_]*. - A valid integer value is
0or an optional minus sign followed by a nonzero ASCII digit and then zero or more ASCII digits. It must fit in a signed 32-bit integer. Leading zeros, a plus sign, an empty value, and extra=characters make an entry invalid. - Ignore every invalid entry without affecting other entries in the same table.
- For each key, the valid value from the table with the greatest priority wins.
- If two tables with that key have the same priority, the table appearing later in
tableswins. - Each table contains at most one entry for any valid key.
Filter rules
Visit selectedKeys in its given order. For every selected key present in the merged configuration, append key=value to the result. Omit selected keys that have no valid merged value.
Process the tables in one pass; do not sort all configuration entries by priority.
Function
mergeAndFilterConfigurations(tables: String[][], priorities: int[], selectedKeys: String[]) → String[]Examples
Example 1
tables = [["a=1","b=2"],["b=3","c=4"]]priorities = [1,2]selectedKeys = ["b","c"]return = ["b=3","c=4"]The second table has greater priority, so b=3 overrides b=2. The merged configuration is {a=1, b=3, c=4}; filtering in [b, c] order returns the shown result.
Example 2
tables = [["rate=10","timeout=bad"],["rate=7","timeout=30"],["rate=2147483648","extra=4"]]priorities = [5,2,10]selectedKeys = ["rate","timeout","extra"]return = ["rate=10","timeout=30","extra=4"]timeout=bad and the overflowing rate entry are invalid and skipped. The valid priority-5 rate=10 remains, while timeout=30 and extra=4 are retained.
Example 3
tables = [["a=1","b=2"],["a=8"],["b=9"]]priorities = [3,3,2]selectedKeys = ["a","b","missing"]return = ["a=8","b=2"]The later equal-priority table wins for a. The priority-3 value b=2 beats b=9 from priority 2, and missing is omitted.
Constraints
tables.length == priorities.length.0 <= tables.length <= 100000.- The total number of entry strings across all tables is at most
200000. - Every entry string has length between
1and100. - Each table contains at most one entry for any valid key.
- Every priority is a signed 32-bit integer.
0 <= selectedKeys.length <= 100000.selectedKeyscontains unique valid ASCII identifier keys.