-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutation.java
More file actions
81 lines (61 loc) · 2.19 KB
/
Permutation.java
File metadata and controls
81 lines (61 loc) · 2.19 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
// Give all possible permutaion of a strinf
// eg str = "abc"
// ans = abc, acb, bac, bca, cab, cba
package RecursionBacktracking.level2;
import java.util.ArrayList;
import java.util.List;
public class Permutation {
// returning ans in repeatetd string causes more space complexity
static void allPermutationStr(String str) {
String ansStr = new String();
helperStr(str, ansStr);
}
static void helperStr(String str, String ans) {
if (str.length() == 0) {
System.out.println(ans);
return;
}
for (int i = 0; i < str.length(); i++) {
char elemet = str.charAt(i);
String newStr = str.substring(0, i) + str.substring(i + 1);
helperStr(newStr, ans + elemet);
}
}
// getting element in the form of ArrayList
static List<String> allPermutationLis(String str) {
String res = new String();
return helperLs(str, res);
}
static List<String> helperLs(String orgStr, String resStr) {
if (orgStr.isEmpty()) {
List<String> list = new ArrayList<>();
list.add(resStr);
return list;
}
List<String> ans = new ArrayList<>();
for (int i = 0; i < orgStr.length(); i++) {
char element = orgStr.charAt(i);
String newStr = orgStr.substring(0, i) + orgStr.substring(i + 1);
ans.addAll(helperLs(newStr, resStr + element));
}
return ans;
}
// count no of permutation
// using basic maths aranging(permutation) n length of stirng into n positions = P(n, n) = n! / (n - n)! = n!
static int countPermutation(String str){
return helperCountPermutation(str, 0);
}
static int helperCountPermutation(String str, int index) {
if (str.length() == 0) {
return 1;
}
int count = str.length();
return count * helperCountPermutation(str.substring(index + 1), index + 1);
}
public static void main(String[] args) {
String str = "abc";
// allPermutationStr(str);
// System.out.println(allPermutationLis(str));
System.out.println(countPermutation(str));
}
}