forked from robinrst/HackerBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionPrint.java
More file actions
156 lines (110 loc) · 2.46 KB
/
Copy pathRecursionPrint.java
File metadata and controls
156 lines (110 loc) · 2.46 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package Lec11;
public class RecursionPrint {
public static void main(String[] args) {
// TODO Auto-generated method stub
// subseq("abc", "");
prmt("amr", "");
}
public static void subseq(String ques, String ans) {
if (ques.length() == 0) {
System.out.println(ans);
return;
}
char ch = ques.charAt(0);
String ros = ques.substring(1);
subseq(ros, ans);
subseq(ros, ans + ch);
}
public static void kpc(String ques, String ans) {
if (ques.length() == 0) {
System.out.println(ans);
return;
}
char ch = ques.charAt(0);
String ros = ques.substring(1);
String code = getCode(ch);
for (int i = 0; i < code.length(); i++) {
kpc(ros, ans + code.charAt(i));
}
}
public static String getCode(char ch) {
if (ch == '1')
return "abc";
else if (ch == '2')
return "def";
else if (ch == '3')
return "ghi";
else if (ch == '4')
return "jk";
else if (ch == '5')
return "lmno";
else if (ch == '6')
return "pqr";
else if (ch == '7')
return "stu";
else if (ch == '8')
return "vwx";
else if (ch == '9')
return "yz";
else if (ch == '0')
return "@#";
else
return "";
}
public static void prmt(String ques, String ans) {
if (ques.length() == 0) {
System.out.println(ans);
return;
}
for (int i = 0; i < ques.length(); i++) {
char ch = ques.charAt(i);
String ros = ques.substring(0, i) + ques.substring(i + 1);
prmt(ros, ans + ch);
}
}
public static void boardPath(int curr, int end, String ans) {
if (curr == end) {
System.out.println(ans);
return;
}
for (int dice = 1; dice <= 6 && curr + dice <= end; dice++) {
boardPath(curr + dice, end, ans + dice);
}
}
public static void mazePathD(int cr, int cc, int ec, int er, String ans) {
if (cr == er && cc == ec) {
System.out.println(ans);
return;
}
if (cr > er || cc > ec) {
return;
}
mazePathD(cr + 1, cc, ec, er, ans + "V");
mazePathD(cr, cc + 1, ec, er, ans + "H");
mazePathD(cr + 1, cc + 1, ec, er, ans + "D");
}
public static void lexicoCounting(int curr, int end) {
if (curr > end) {
return;
}
System.out.println(curr);
int i = 0;
if (curr == 0) {
i = 1;
}
while (i < 10) {
lexicoCounting(curr * 10 + i, end);
i++;
}
}
public static int coinToss(int n, String ans) {
if (n == 0) {
System.out.println(ans);
return 1;
}
int cnt = 0;
cnt += coinToss(n - 1, ans + "H");
cnt += coinToss(n - 1, ans + "T");
return cnt;
}
}