-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPermutations.java
More file actions
55 lines (37 loc) · 1.02 KB
/
Copy pathStringPermutations.java
File metadata and controls
55 lines (37 loc) · 1.02 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
import java.util.ArrayList;
import java.util.List;
public class StringPermutations {
public static void main(String[] args) {
String test = "abcd";
stringPermutations(test);
}
private static List<char[]> stringPermutations(String inputStr){
char[] input = inputStr.toCharArray();
// System.out.println(input);
int[] visited = new int[input.length];
ArrayList<char[]> result = new ArrayList<char[]>();
for(int i = 0; i < visited.length; i++){
visited[i] = 0;
}
char[] word = new char[input.length];
doPermutations(input, visited, word, 0, input.length);
return result;
}
private static void doPermutations(char[] input, int[] visited, char[] word, int count, int length){
if(count == length){
System.out.println(word);
return;
}
for(int i = 0; i < length; i++){
if(visited[i] == 1){
continue;
}
else{
word[count] = input[i];
visited[i] = 1;
doPermutations(input, visited, word, count + 1, length);
visited[i] = 0;
}
}
}
}