-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctPermutationsOfString
More file actions
33 lines (26 loc) · 1.01 KB
/
DistinctPermutationsOfString
File metadata and controls
33 lines (26 loc) · 1.01 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
import java.util.HashSet;
import java.util.Set;
public class StringPermutationExample {
public static void main(String[] args) {
String input = "hlllo";
String input2 = "where";
Set<String> set = new HashSet<>();
permutation(input, "", set);
System.out.println(set);
set.clear();
permutation(input2, "", set);
System.out.println(set);
}
private static void permutation(String input, String sofar, Set<String> set) {
if (input.equals("")) { //this becomes true when input.length becomes 0
set.add(sofar); // and so it is added to the set
}
int j =0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (input.indexOf(c, i + 1) != -1)
continue; //this i+1 helps in getting index -1 when the input.length completes
permutation(input.substring(0, i) + input.substring(i + 1), sofar + c, set);
}
}
}