-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPemutations.java
More file actions
97 lines (61 loc) · 2.1 KB
/
Pemutations.java
File metadata and controls
97 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.vinay.practice.lc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// https://leetcode.com/problems/permutations/description/
// Input: nums = [1,2,3]
// Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
public class Pemutations {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = new int[] {1,2,3};
String numbers = Arrays.toString(nums).replaceAll("[^\\d\\s]", "").replace(" ", "");
printStringPermutations("", numbers);
System.out.println("List of permutations string : " + stringPermutationsList("", numbers ));
List<List<Integer>> list = new ArrayList<>();
permuteArrayInt(new ArrayList<Integer>() , list, nums);
System.out.println("List of list of permutations string : ");
list.forEach(e -> System.out.println(e));
}
public static void permuteArrayInt(List<Integer> curr, List<List<Integer>> list, int[] nums) {
if(curr.size() == nums.length) {
list.add(new ArrayList<>(curr));
return;
}
for (int num : nums) {
if(!curr.contains(num)) {
curr.add(num);
permuteArrayInt(curr, list, nums);
curr.remove(curr.size()-1);
}
}
}
public static void printStringPermutations(String p, String up) {
if (up.isEmpty()) {
System.out.println(p);
return;
}
char ch = up.charAt(0);
for(int i=0; i<=p.length(); i++) {
String first = p.substring(0,i);
String second = p.substring(i, p.length());
printStringPermutations(first + ch + second, up.substring(1));
}
}
public static ArrayList<String> stringPermutationsList(String p, String up) {
if (up.isEmpty()) {
System.out.println(p);
ArrayList<String> temp = new ArrayList<>();
temp.add(p);
return temp;
}
ArrayList<String> list = new ArrayList<>();
char ch = up.charAt(0);
for(int i=0; i<=p.length(); i++) {
String first = p.substring(0,i);
String second = p.substring(i, p.length());
list.addAll(stringPermutationsList(first + ch + second, up.substring(1)));
}
return list;
}
}