mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
29 lines
717 B
JavaScript
29 lines
717 B
JavaScript
/**
|
|
* @param {*[]} comboOptions
|
|
* @param {number} comboLength
|
|
* @return {*[]}
|
|
*/
|
|
export default function combineWithRepetitions(comboOptions, comboLength) {
|
|
if (comboLength === 1) {
|
|
return comboOptions.map(comboOption => [comboOption]);
|
|
}
|
|
|
|
// Init combinations array.
|
|
const combos = [];
|
|
|
|
// Eliminate characters one by one and concatenate them to
|
|
// combinations of smaller lengths.
|
|
comboOptions.forEach((currentOption, optionIndex) => {
|
|
const smallerCombos = combineWithRepetitions(
|
|
comboOptions.slice(optionIndex),
|
|
comboLength - 1,
|
|
);
|
|
|
|
smallerCombos.forEach((smallerCombo) => {
|
|
combos.push([currentOption].concat(smallerCombo));
|
|
});
|
|
});
|
|
|
|
return combos;
|
|
}
|