-
Notifications
You must be signed in to change notification settings - Fork 4
/
27-jun.js
90 lines (67 loc) · 1.58 KB
/
27-jun.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function tilingProblem(n) {
if(n === 1 || n === 2)
return n;
return tilingProblem(n-1) + tilingProblem(n-2);
}
//console.log(tilingProblem(5));
function subsequences(arr, index, curr) {
if(index === arr.length) {
console.log(curr);
return;
}
//exclusion
subsequences(arr, index+1, curr);
//inclusion
curr.push(arr[index]);
subsequences(arr, index+1, curr);
//poping
curr.pop();
}
function balancedParanthesis(n, open, close, str) {
if(open + close === 2*n){
console.log(str);
return;
}
if(open < n) {
balancedParanthesis(n, open+1, close, str+'(');
}
if(open > close) {
balancedParanthesis(n, open, close+1, str+')');
}
}
//balancedParanthesis(3, 0, 0, '');
function minkiWedsChintu(matrix, row, col) {
// out of range.
if(row === -1 || col === -1)
return 0;
if(matrix[row][col] === -1)
return 0;
// minki reached to chintu
if(row === 0 && col === 0) {
return 1;
}
return minkiWedsChintu(matrix, row-1, col)
+ minkiWedsChintu(matrix, row, col-1);
}
// let ways = minkiWedsChintu([
// [0, 0, -1],
// [0, 0, 0],
// [-1, 0, 0]
// ], 2, 2);
// console.log(ways);
function permutations(str, l, r) {
if(l === r) {
console.log(str);
}
for(let i=l; i<=r; i++) {
permutations(swap(str, l, i), l+1, r);
}
}
function swap(str, l, i) {
let arr = str.split('');
let temp = arr[l];
arr[l] = arr[i];
arr[i] = temp;
return arr.join('');
}
permutations('ABC', 0, 2);