I wish to create all permutations of all digits from a function.
For e.g. I have the array [1,2,3]. I would like the function to first print all possible permutations of 1 digit numbers:
1
2
3
then all possible permutations of 2-digit numbers(duplicates like 11, 22, 33 included)
11
12
13
21
22
23
31
32
33
and similarly for the 3 digit numbers.
I want to include all sort of possible permutations(11, 111, 222 etc.) up to the number of digits in the array, like in this example, I would like to have all permutations up to 3 digits. How can I write it?
My current code creates only exclusive permutations of only the number of digits asked, not the smaller digits:
public class Permutation {
public static void main(String[] args)
{
String str = "132546";
int n = str.length();
Permutation permutation = new Permutation();
permutation.permute(str, 0, n-1);
}
private void permute(String str, int l, int r)
{
if (l == r)
System.out.println(str);
else
{
for (int i = l; i <= r; i )
{
str = swap(str,l,i);
permute(str, l 1, r);
str = swap(str,l,i);
}
}
}
public String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
}
I want it to create all permutations as written above.
CodePudding user response:
Try this:
import java.util.Arrays;
class Permutation {
public static void main(String[] args){
String str = "123456";
Permutation permutation = new Permutation();
permutation.printAll(str);
}
private void printAll(String str) {
String [][] table;
int n = str.length();
for(int k=1;k<=n;k ) {//k - number of digits
System.out.println("=================");
System.out.println(k " digit(s):");
printPermutationsWithRepet(str,k);
}
}
private void printPermutationsWithRepet(String str, int k) {
String [] a =new String [k];
printPermutations(str,k,0,1, a);
}
private void printPermutations(String str, int k, int startPosition, int maxUsed, String [] a) {
if(startPosition == k) {
System.out.println(Arrays.toString(a));
} else {
for(int i = maxUsed; i <= str.length(); i ) {
a[startPosition] = str.charAt(i-1) "";
printPermutations(str,k,startPosition 1,i, a);
}
}
}
}
CodePudding user response:
Note that the way the digits change is like the odometer on a car. So one way to go is "simulate" the odometer. No fancy recursion needed.
class Odometer {
private final int[] digits = {1, 2, 3};
/** Indices into the digits array. Initially all zero. */
private final int[] indices = new int[2];
/** Advance to the next value, returning whether it's a non-repeat. */
boolean next() {
for (int i = 0; i < indices.length; i) {
// If incrementing the current digit index causes it to "wrap."
if ( indices[i] == digits.length) {
indices[i] = 0; // It wrapped. Reset this index and keep going.
} else {
return true; // Otherwise it didn't wrap. We're done.
}
}
return false; // All indices have wrapped back to zero.
}
/** Print current digits. */
void print() {
for (int i = 0; i < indices.length; i) {
System.out.print(digits[indices[i]]);
}
System.out.println();
}
public static void main(String[] args) {
Odometer odo = new Odometer();
do {
odo.print();
} while (odo.next());
}
}
A more general comment is that this isn't a permutation problem. It appears your initial solution is based on that mistake. The code is for the all permutations problem. Consequently it's not close to correct. If that's the result of Googling for "permutations" and copy/pasting code you didn't understand, pause and take stock. This is not a useful strategy. It will end badly.