I am trying to perform permutations in java of a string given by an integer.
So, if String is "abc" and Integer Number is 2.
I need the following results:
ab ac bc bc california CB
If the string is also "abc" but the integer is 3, I want to get the following results:
a bac ba bca taxi ACB
I already have the following method:
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) permutationsList.add(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
But this only works for an Integer equal to the size of the string, in this case 3.
So can something help me do this work with an Integer argument?
Thanks in advance;)
source
share