-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathcombineWithRepetitions.js
More file actions
31 lines (27 loc) · 1.09 KB
/
Copy pathcombineWithRepetitions.js
File metadata and controls
31 lines (27 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @param {*[]} comboOptions
* @param {number} comboLength
* @return {*[]}
*/
export default function combineWithRepetitions(comboOptions, comboLength) {
// Nếu độ dài của tổ hợp là 1 thì mỗi phần tử của mảng gốc là tổ hợp của chính nó.
if (comboLength === 1) {
return comboOptions.map((comboOption) => [comboOption]);
}
// Tạo mảng tổ hợp.
const combos = [];
// Ghi nhớ từng ký tự và nối chúng thành các tổ hợp có độ dài nhỏ hơn.
// Không trích xuất phần tử vì ở đây được lặp lại.
comboOptions.forEach((currentOption, optionIndex) => {
// Tạo tổ hợp có kích thước nhỏ hơn.
const smallerCombos = combineWithRepetitions(
comboOptions.slice(optionIndex),
comboLength - 1,
);
// Nối currentOption vào tất cả tổ hợp có kích thước nhỏ hơn.
smallerCombos.forEach((smallerCombo) => {
combos.push([currentOption].concat(smallerCombo));
});
});
return combos;
}