forked from Itachi-Ucchiha/BasicDSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8thJune(I).java
More file actions
46 lines (36 loc) · 875 Bytes
/
Copy path8thJune(I).java
File metadata and controls
46 lines (36 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
45
46
public class StringPermutations {
static int factorial(int n){
int val = 1;
for(int i=2; i<=n;i++){
val*=i;
}
return val;
}
static void solution(String str){
int n = str.length();
int f = factorial(n);
for(int i =0; i<f; i++){
StringBuilder sb = new StringBuilder(str);
int temp = i;
for(int j =n ; j>=1; j--){
int q = temp/j;
int r = temp%j;
System.out.print(sb.charAt(r));
sb.deleteCharAt(r);
temp = q;
}
System.out.println();
}
}
public static void main(String[] args) {
String str = "abc";
solution(str);
}
}
// =================== Output =======================
// abc
// bac
// cab
// acb
// bca
// cba