-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path30-mar.js
42 lines (31 loc) · 825 Bytes
/
30-mar.js
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
39
40
41
42
// subsequence
function subsequence(arr, index, curr) {
if(index === arr.length) {
console.log(curr);
return;
}
subsequence(arr, index+1, curr); //exclusion
curr.push(arr[index]);
subsequence(arr, index+1, curr); // inclusion
curr.pop();
}
subsequence([1,2], 0, []);
// permutations of a string
function permute(chintu, str, lastInd) {
if(chintu === lastInd) {
console.log(str);
return
}
for(let pintu=chintu;pintu<=lastInd;pintu++) {
permute(chintu+1, swap(chintu, pintu, str), lastInd);
}
}
permute(0, 'ABC', 2);
function swap(chintu, pintu, str) {
let charArray = str.split("");
let temp;
temp = charArray[chintu];
charArray[chintu] = charArray[pintu];
charArray[pintu] = temp;
return charArray.join("");
}