-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutation.java
30 lines (26 loc) · 954 Bytes
/
Permutation.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
import java.util.ArrayList;
import java.util.List;
/**
* Code found:
* http://codereview.stackexchange.com/questions/41510/calculate-all-possible-combinations-of-given-characters
* @author SZupancic
*/
public class Permutation {
// Returns the list
static List strings = new ArrayList();
public List possibleStrings(int maxLength, char[] alphabet, String curr) {
// If the current string has reached it's maximum length
if (curr.length() == maxLength) {
strings.add(curr);
// Else add each letter from the alphabet to new strings and process these new strings again
} else {
for (int i = 0; i < alphabet.length; i++) {
String oldCurr = curr;
curr += alphabet[i];
possibleStrings(maxLength, alphabet, curr);
curr = oldCurr;
}
}
return strings;
}
}