Expand Attribute Combinations
Learn this problemProblem statement
A product configuration has distinct attribute names in keys. For each index i, values[i] lists the allowed values for keys[i].
Return every assignment in the Cartesian product. Represent one assignment as a row whose value at index i belongs to keys[i]. Preserve the input key order within each row, and enumerate rows by choosing values from left to right in their given order.
If there are no keys, return one empty assignment. If any attribute has no allowed values, return no assignments.
Function
expandAttributeCombinations(keys: String[], values: String[][]) → String[][]Examples
Example 1
keys = ["color","size"]values = [["red","blue"],["S","M"]]return = [["red","S"],["red","M"],["blue","S"],["blue","M"]]Each color is paired with each size. The first attribute changes more slowly because combinations are enumerated from left to right.
Example 2
keys = ["region","tier","mode"]values = [["us"],["free","pro"],["light","dark"]]return = [["us","free","light"],["us","free","dark"],["us","pro","light"],["us","pro","dark"]]The single region appears in every row, while the two later attributes generate four combinations.
Example 3
keys = []values = []return = [[]]The Cartesian product of zero attribute sets contains one empty assignment.
Constraints
0 <= keys.length == values.length <= 12.- Every key is non-empty, and keys are distinct.
- Each value is a non-empty string; values within one attribute are distinct.
- The total number of returned assignments is at most
100000.