Convert Sparse Records to a Filled Table
Problem statement
Each record is represented by parallel arrays keysByRow[i] and valuesByRow[i]. Keys are unique within a record, while different records may contain different keys.
Return a rectangular table. Its first row is the lexicographically sorted union of all keys. Every following row corresponds to one input record in original order and contains its value under each header key, or fill when that key is absent.
If there are no records, return one empty header row.
Function
recordsToTable(keysByRow: String[][], valuesByRow: String[][], fill: String) → String[][]Examples
Example 1
keysByRow = [["name","age"],["name","city"]]valuesByRow = [["Ada","36"],["Lin","Paris"]]fill = ""return = [["age","city","name"],["36","","Ada"],["","Paris","Lin"]]The header is the sorted key union and missing cells use fill.
Example 2
keysByRow = [["b"],[],["a","b"]]valuesByRow = [["2"],[],["1","3"]]fill = "NA"return = [["a","b"],["NA","2"],["NA","NA"],["1","3"]]Empty records still produce a fully filled row.
Example 3
keysByRow = []valuesByRow = []fill = "x"return = [[]]With no records, the table contains only an empty header.
Constraints
0 <= keysByRow.length == valuesByRow.length <= 100000.- For each row, key and value counts are equal and keys are unique.
- Keys are nonempty case-sensitive strings of at most 100 characters.
- The total number of key-value pairs is at most
200000.