-
-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathgeneratepermutations.ts
More file actions
38 lines (33 loc) · 926 Bytes
/
generatepermutations.ts
File metadata and controls
38 lines (33 loc) · 926 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
32
33
34
35
36
37
38
/*
* Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order);
*
* What is permutations?
* - Permutation means possible arrangements in a set (here it is an array);
*
* Reference to know more about permutations:
* - https://www.britannica.com/science/permutation
*
*/
const swap = <T>(arr: T[], i: number, j: number): T[] => {
const newArray: T[] = [...arr]
const temp: T = newArray[i]
newArray[i] = newArray[j]
newArray[j] = temp
return newArray
}
const permutations = <T>(arr: T[]): T[][] => {
const P: T[][] = []
const permute = (arr: T[], low: number, high: number): T[][] => {
if (low === high) {
P.push([...arr])
return P
}
for (let i = low; i <= high; i++) {
arr = swap(arr, low, i)
permute(arr, low + 1, high)
}
return P
}
return permute(arr, 0, arr.length - 1)
}
export { permutations }