-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPermutator.java
More file actions
executable file
·70 lines (59 loc) · 2.06 KB
/
StringPermutator.java
File metadata and controls
executable file
·70 lines (59 loc) · 2.06 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
import java.util.Scanner;
public class StringPermutator {
/*
Find list of factorials.
input: integer number. example: number=3
output: List of factorials, example: factorials=[1,1*2,1*2*3]
*/
public static int [] getFactorialsList(int number) {
if(number<0){
return null;
}
int [] factorials = new int[number+1];
factorials[0] = 1;
for (int i = 1; i<=number;i++) {
factorials[i] = factorials[i-1] * i;
}
return factorials;
}
/*
Find String Permutations.
input: string to permutate and list of factorials for the input string.
output: list of permutations.
*/
public static String [] findPermutations(String string,int [] factorials) {
String [] resutls= new String[factorials[string.length()]+1];
for (int i = 0; i < factorials[string.length()]; i++) {
String onePermutation="";
String temp = string;
int positionCode = i;
for (int position = string.length(); position > 0 ;position--){
int selected = positionCode / factorials[position-1];
onePermutation += temp.charAt(selected);
positionCode = positionCode % factorials[position-1];
temp = temp.substring(0,selected) + temp.substring(selected+1);
}
resutls[i]=onePermutation;
}
return resutls;
}
/*
print list of strings.
input: array of Strings and the size of the list.
output: print all list elements on the screen.
*/
public static void printStrings(String[] strings,int size) {
for (int i = 0; i < size; i++) {
System.out.println(strings[i]);
}
}
public static void main(String[] args) {
System.out.print("Enter string to permutate: ");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
scanner.close();
int [] factorials =getFactorialsList(string.length());
String [] permutations=findPermutations(string,factorials);
printStrings(permutations,factorials[string.length()]);
}
}