-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathPermutationsOfString.java
More file actions
47 lines (38 loc) · 1.49 KB
/
PermutationsOfString.java
File metadata and controls
47 lines (38 loc) · 1.49 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
// Code to illustrate Backtracking in JAVA
// Here the example used is Permuation of Strings
// We will print all the permuations of Strings, the core algorithm at work here is backtracking
import java.util.*;
import java.io.*;
class Permutations{
// This set will store all the permutations of the given string
// I have used Set here to avoid repetitions in cases of strings which might have duplicate letters
static Set<String> stringPermutations = new HashSet<>();
// Main idea of this function is to keep one variable fixed and keep swapping the rest
// until we have completed the cycle.
private static void backtrack(String str, int l, int r){
if(l == r){
stringPermutations.add(str);
}
else{
for(int i=l; i<=r; i++){
str = swap(str, i, l);
backtrack(str, l+1, r);
str = swap(str, i, l);
}
}
}
private static String swap(String str, int x, int y){
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(x, str.charAt(y));
sb.setCharAt(y, str.charAt(x));
return sb.toString();
}
public static void main(String[] args){
String str = "cat";
backtrack(str, 0, str.length()-1);
System.out.println("Below are the permutations for string : "+ str);
for(String p : stringPermutations){
System.out.print(p + " ");
}
}
}