Permutations of an Array in Java
Here's an example Java code for generating all permutations of an array using recursion:
public class ArrayPermutations {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
generatePermutations(arr, 0, arr.length - 1);
}
public static void generatePermutations(int[] arr, int left, int right) {
if (left == right) {
printArray(arr);
} else {
for (int i = left; i <= right; i++) {
// swap
swap(arr, left, i);
generatePermutations(arr, left + 1, right);
// backtract
swap(arr, left, i);
}
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
This code defines a generatePermutations
method which takes an array, the left index, and the right index as parameters. It uses a recursive approach where it swaps each element with the first element (at index left
), generates all permutations of the remaining elements, and then swaps back the elements to their original positions.
The printArray
method is used to print each permutation as it is generated.
To use this code, you can create an array of integers (or any other data type) and call the generatePermutations
method with the appropriate indices. In the example code above, we create an array of {1, 2, 3}
and call generatePermutations
with left = 0
and right = arr.length - 1
.
Here's an example Java code for generating all permutations of an array using recursion and backtracking:
import java.util.*;
public class ArrayPermutations {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
generatePermutations(arr);
}
public static void generatePermutations(int[] arr) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), arr);
for (List<Integer> permutation : result) {
for (int num : permutation) {
System.out.print(num + " ");
}
System.out.println();
}
}
public static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] arr) {
if (tempList.size() == arr.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < arr.length; i++) {
if (tempList.contains(arr[i])) {
continue;
}
tempList.add(arr[i]);
backtrack(result, tempList, arr);
tempList.remove(tempList.size() - 1);
}
}
}
}
This code defines a generatePermutations
method which takes an array as a parameter. It creates an empty list to store the permutations and then calls the backtrack
method with an empty list and the input array.
The backtrack
method recursively generates all permutations by adding each element to the temporary list, calling itself with the remaining elements, and then removing the added element before trying the next element. This approach ensures that each element is used only once in each permutation.
Once all permutations have been generated, the code prints them to the console.
To use this code, you can create an array of integers (or any other data type) and call the generatePermutations
method with that array. In the example code above, we create an array of {1, 2, 3}
and call generatePermutations
with that array.
To generate all permutations of a string, you can use a similar approach to the array permutation example I provided earlier, but with a string input instead of an array. Here's an example Java code for generating all permutations of a string using recursion:
public class StringPermutations {
public static void main(String[] args) {
String str = "abc";
generatePermutations("", str);
}
public static void generatePermutations(String prefix, String str) {
int n = str.length();
if (n == 0) {
System.out.println(prefix);
} else {
for (int i = 0; i < n; i++) {
generatePermutations(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
}
}
This code defines a generatePermutations
method which takes a prefix string and a string as parameters. It uses a recursive approach where it adds each character of the input string to the prefix, generates all permutations of the remaining characters, and then removes the added character before trying the next character. This approach ensures that each character is used only once in each permutation.
The print
statement in the base case is used to print each permutation as it is generated.
To use this code, you can call the generatePermutations
method with an empty prefix string and the input string you want to permute. In the example code above, we create a string of "abc"
and call generatePermutations
with an empty prefix string and that input string.