forked from 14visheshjain/DataStructure-Algorithm-in-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtossWithNoCH.java
More file actions
44 lines (38 loc) · 875 Bytes
/
tossWithNoCH.java
File metadata and controls
44 lines (38 loc) · 875 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
39
40
41
42
43
44
package lecture9;
import java.util.Scanner;
public class tossWithNoCH {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
int n = a.nextInt();
toss(n, "" , true);
}
// public static void toss(int n, String ans) {
// if (n == 0) {
// System.out.println(ans);
// return;
// }
// if (ans.length() == 0) {
// String myansH = ans + "H";
// toss(n - 1, myansH);
// }
// if (ans.length() > 0 && ans.charAt(ans.length() - 1) != 'H') {
// String myansH = ans + "H";
// toss(n - 1, myansH);
// }
// String myansT = ans + "T";
// toss(n - 1, myansT);
//
// }
public static void toss(int n, String ans , boolean flag) {
if (n == 0) {
System.out.println(ans);
return;
}
if (flag) {
String myansH = ans + "H";
toss(n - 1, myansH , false);
}
String myansT = ans + "T";
toss(n - 1, myansT , true);
}
}