-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPermutation.java
82 lines (76 loc) · 2.18 KB
/
Permutation.java
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
/*https://leetcode.com/problems/permutations/*/
/*https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string-1587115620/1*/
//for integer array
class Solution {
List<List<Integer>> list;
public List<List<Integer>> permute(int[] nums) {
list = new ArrayList<List<Integer>>();
//recursion call
recurPerm(nums,0,nums.length-1);
return list;
}
public void recurPerm(int[] nums, int l, int r) {
//base case
if (l == r)
{
//add the permutation to the list
list.add(new ArrayList<Integer>());
for (int i = 0; i < nums.length; ++i)
list.get(list.size()-1).add(nums[i]);
}
else
{
//for every integer in the range
for (int i = l; i <= r; ++i)
{
nums = swap(nums,l,i); //backtracking step-1 - swap
recurPerm(nums,l+1,r); //backtracking step-2 - recur
nums = swap(nums,l,i); //backtracking step-3 - backtrack
}
}
}
public int[] swap(int[] nums, int l, int i) {
int temp = nums[l];
nums[l] = nums[i];
nums[i] = temp;
return nums;
}
}
//for string
class Solution
{
ArrayList<String> list;
public ArrayList<String> permutation(String S)
{
list = new ArrayList<String>();
//recursion call
recurPerm(S,0,S.length()-1);
//sort and return
Collections.sort(list);
return list;
}
public void recurPerm(String str, int l, int r)
{
//base case
if (l == r)
list.add(str);
else
{
//for every character in the range
for (int i = l; i <= r; ++i)
{
str = swap(str,l,i); //backtracking step-1 - swap
recurPerm(str,l+1,r); //backtracking step-2 - recur
str = swap(str,l,i); //backtracking step-3 - backtrack
}
}
}
public String swap(String s, int i, int j)
{
char[] temp = s.toCharArray();
char ch = temp[i];
temp[i] = temp[j];
temp[j] = ch;
return String.valueOf(temp);
}
}