mirror of https://github.com/evanferrao/dsa
Array/String permutation
This commit is contained in:
parent
a51e635139
commit
aee4a4676c
|
|
@ -0,0 +1,60 @@
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
class ArrayPermutationOne {
|
||||||
|
|
||||||
|
private void recurPermute(int[] nums, List<Integer> ds, List<List<Integer>> finalAnswer, boolean[] freq) {
|
||||||
|
|
||||||
|
if (ds.size() == nums.length){
|
||||||
|
finalAnswer.add(new ArrayList<>(ds));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<nums.length; i++){
|
||||||
|
if(!freq[i]){
|
||||||
|
freq[i]=true;
|
||||||
|
ds.add(nums[i]);
|
||||||
|
recurPermute(nums, ds, finalAnswer, freq);
|
||||||
|
freq[i]=false;
|
||||||
|
ds.remove(ds.size()-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<List<Integer>> permute(int[] nums) {
|
||||||
|
List<List<Integer>> finalAnswer = new ArrayList<>();
|
||||||
|
List<Integer> ds = new ArrayList<>();
|
||||||
|
boolean[] freq = new boolean[nums.length];
|
||||||
|
|
||||||
|
recurPermute(nums, ds, finalAnswer, freq);
|
||||||
|
return finalAnswer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
// Input size
|
||||||
|
System.out.print("Enter number of elements: ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
|
||||||
|
int[] nums = new int[n];
|
||||||
|
|
||||||
|
// Input array elements
|
||||||
|
System.out.println("Enter elements:");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
nums[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
Solution obj = new Solution();
|
||||||
|
List<List<Integer>> result = obj.permute(nums);
|
||||||
|
|
||||||
|
// Print permutations
|
||||||
|
System.out.println("Permutations:");
|
||||||
|
for (List<Integer> perm : result) {
|
||||||
|
System.out.println(perm);
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
class ArrayPermutationTwo {
|
||||||
|
|
||||||
|
public void recurPermute(int index, int nums[], List<List<Integer>> finalAnswer){
|
||||||
|
if (index == nums.length){
|
||||||
|
ArrayList<Integer> ds = new ArrayList<>();
|
||||||
|
for(int i=0; i<nums.length; i++){
|
||||||
|
ds.add(nums[i]);
|
||||||
|
}
|
||||||
|
finalAnswer.add(new ArrayList<>(ds));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=index; i<nums.length; i++){
|
||||||
|
swap(index, i, nums);
|
||||||
|
recurPermute(index+1, nums, finalAnswer);
|
||||||
|
swap(index, i, nums);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void swap(int x, int y, int nums[]){
|
||||||
|
int temp = nums[x];
|
||||||
|
nums[x]=nums[y];
|
||||||
|
nums[y]=temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<List<Integer>> permute(int[] nums) {
|
||||||
|
List<List<Integer>> finalAnswer = new ArrayList<>();
|
||||||
|
recurPermute(0, nums, finalAnswer);
|
||||||
|
return finalAnswer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
// Input size
|
||||||
|
System.out.print("Enter number of elements: ");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
|
||||||
|
int[] nums = new int[n];
|
||||||
|
|
||||||
|
// Input elements
|
||||||
|
System.out.println("Enter elements:");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
nums[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
Solution obj = new Solution();
|
||||||
|
List<List<Integer>> result = obj.permute(nums);
|
||||||
|
|
||||||
|
// Print permutations
|
||||||
|
System.out.println("Permutations:");
|
||||||
|
for (List<Integer> perm : result) {
|
||||||
|
System.out.println(perm);
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue