Array/String permutation

This commit is contained in:
Evan Ferrao 2026-02-05 22:51:47 +05:30 committed by GitHub
parent a51e635139
commit aee4a4676c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 122 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}