-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha43recursion1.java
More file actions
79 lines (73 loc) · 2.37 KB
/
a43recursion1.java
File metadata and controls
79 lines (73 loc) · 2.37 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
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
public class a43recursion1 {
// Tiling problem (2xn) board.
public static int tilingProb(int n){
// Base Case
if (n==0 || n==1) {
return 1;
}
// vertical choice
int fnm1 = tilingProb(n-1);
System.out.println("vertical ways"+fnm1);
// horizontal choice
int fnm2 = tilingProb(n-2);
System.out.println("horizontal ways"+fnm2);
int totalWays = fnm1 + fnm2;
System.out.println("total ways-"+totalWays);
return totalWays;
}
// Remove duplicates
public static void removeDup(String str, int idx, StringBuilder newStr,boolean map[]){
if (idx == str.length()) {
System.out.println(newStr);
return;
}
char currChar = str.charAt(idx);
if (map[currChar-'a']==true) {
removeDup(str, idx+1, newStr, map);
} else {
map[currChar-'a'] = true;
removeDup(str, idx+1, newStr.append(currChar), map);
}
}
// friends pairing problem
public static int friendsPairingProb(int n){
// now base case
if (n==1 || n==2) {
return n;
}
// for single case
// int fnm1 = friendsPairingProb(n-1);
// // for pair
// int fnm2 = friendsPairingProb(n-2);
// int totPairWays = (n-1)*fnm2;
// return fnm1 + totPairWays;
return friendsPairingProb(n-1) + (n-1)*friendsPairingProb(n-2);
}
//Binary String Problem
static void BinaryProb(int n,int lastPlace,String str){
if (n==0) {
System.out.println(str);
return;
}
// if (lastPlace == 0) {
// BinaryProb(n-1, 0, str+'0');
// BinaryProb(n-1, 1, str+'1');
// }else{
// BinaryProb(n-1, 0, str+'0');
// }
// Simplified
BinaryProb(n-1, 0, str+'0');
if (lastPlace == 0) {
BinaryProb(n-1, 1, str+'1');
}
}
public static void main(String[] args) {
int n = 4;
// System.out.println(tilingProb(n));
boolean map[] = new boolean[26];
StringBuilder sb = new StringBuilder();
removeDup("bcdebcdea", 0, sb, map);
// System.out.println(friendsPairingProb(3));
// BinaryProb(3,0,"");
}
}