-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathPrintPemutations.java
More file actions
92 lines (62 loc) · 1.64 KB
/
PrintPemutations.java
File metadata and controls
92 lines (62 loc) · 1.64 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
package Recursioncontd;
public class PrintPemutations {
public static void main(String[] args) {
String s = "abc";
PrintPermutations2(s, "");
StringBuilder s1 = new StringBuilder("abc");
StringBuilder s2 = new StringBuilder();
//PrintPermutations1SB(s1, s2);
//PrintPermutations1SB(s1, s2);
}
public static void PrintPermutations2(String str, String asf) {
if(str.length()==0) {
System.out.println(asf);
return;
}
char ch = str.charAt(0);
String roq = str.substring(1);
for(int i=0; i<=asf.length(); i++) {
String l = asf.substring(0, i);
String r = asf.substring(i);
PrintPermutations2(roq, r+ch+l);
}
}
public static void PrintPermutations1(String str, String asf) {
if(str.length()==0) {
System.out.println(asf);
return;
}
for(int i=0; i<str.length(); i++) {
char ch = str.charAt(i);
PrintPermutations1(str.substring(0,i)+str.substring(i+1), asf+ch);
}
}
public static void PrintPermutations1SB(StringBuilder s1, StringBuilder s2) {
if(s1.length() == 0) {
System.out.println(s2);
return;
}
char ch = s1.charAt(0);
s1.deleteCharAt(0);
for(int i=0; i<=s2.length(); i++) {
s2.insert(i, ch);
PrintPermutations1SB(s1, s2);
s2.deleteCharAt(i);
}
s1.insert(0, ch);
}
public static void PrintPermutations2SB(StringBuilder s1, StringBuilder s2) {
if(s1.length() ==0) {
System.out.println(s2);
return;
}
for(int i=0; i<s1.length(); i++) {
char ch = s1.charAt(i);
s1.deleteCharAt(i);
s2.insert(0, ch);
PrintPermutations2SB(s1, s2);
s2.deleteCharAt(0);
s1.insert(i, ch);
}
}
}