-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathpermutateWithRepetitions.js
More file actions
31 lines (27 loc) · 912 Bytes
/
Copy pathpermutateWithRepetitions.js
File metadata and controls
31 lines (27 loc) · 912 Bytes
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 {*[]} permutationOptions
* @param {number} permutationLength
* @return {*[]}
*/
export default function permutateWithRepetitions(
permutationOptions,
permutationLength = permutationOptions.length,
) {
if (permutationLength === 1) {
return permutationOptions.map((permutationOption) => [permutationOption]);
}
// Khởi tạo mảng hoán vị.
const permutations = [];
// Lấy hoán vị nhỏ nhất.
const smallerPermutations = permutateWithRepetitions(
permutationOptions,
permutationLength - 1,
);
// Đi tới tất cả lựa chọn và kết hợp nó với hoán vị nhỏ nhất.
permutationOptions.forEach((currentOption) => {
smallerPermutations.forEach((smallerPermutation) => {
permutations.push([currentOption].concat(smallerPermutation));
});
});
return permutations;
}