-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallPermutations.java
40 lines (32 loc) · 1.01 KB
/
allPermutations.java
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
package Backtracking;
import java.util.HashSet;
import java.util.Set;
public class allPermutations {
public static Set<String> crunchifyPermutation(String str) {
Set<String> crunchifyResult = new HashSet<String>();
if (str == null) {
return null;
} else if (str.length() == 0) {
crunchifyResult.add("");
return crunchifyResult;
}
char firstChar = str.charAt(0);
String rem = str.substring(1);
Set<String> words = crunchifyPermutation(rem);
for (String newString : words) {
for (int i = 0; i <= newString.length(); i++) {
crunchifyResult.add(crunchifyCharAdd(newString, firstChar, i));
}
}
return crunchifyResult;
}
public static String crunchifyCharAdd(String str, char c, int j) {
String first = str.substring(0, j);
String last = str.substring(j);
return first + c + last;
}
public static void main(String[] args) {
String str = "abc";
System.out.println("\nString: " + str + "\nPermutations: " + crunchifyPermutation(str));
}
}