# 1215. Algorithm - Combination and PermutationCombination, Permutation, and Subset

Implement DFS for combination, permutation and subset.

## 1. Combination

### 1.1 Description

Given two integers n and k, return all possible combinations of k numbers out of 1 … n.

Example:

Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]


### 1.2 DFS Implementation

public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new ArrayList<>();
if (n <= 0 || k <= 0 || n < k) {
return ans;
}

List<Integer> list = new ArrayList<Integer>();
dfs(n, k, 1, list, ans);

return ans;
}

private void dfs(int n, int k, int pos, List<Integer> list, List<List<Integer>> ans) {
if (list.size() == k) {
return;
}

for(int i = pos; i <= n; i++) {
dfs(n, k, i + 1, list, ans);
list.remove(list.size() - 1);
}
}


## 2. Permutation

### 2.1 Description

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]


### 2.2 DFS

public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
if (nums == null || nums.length == 0) {
return ans;
}

boolean[] visited = new boolean[nums.length];
dfs(nums, visited, new ArrayList<>(), ans);
return ans;
}

private void dfs(int[] nums, boolean[] visited, List<Integer> list, List<List<Integer>> ans) {
if (list.size() == nums.length) {
return;
}

for (int i = 0; i < nums.length; i++) {
if (visited[i]) {
continue;
}
visited[i] = true;
dfs(nums, visited, list, ans);
list.remove(list.size() - 1);
visited[i] = false;
}
}


## 3. Subsets

### 3.1 Description

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]


### 3.2 DFS

public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
if (nums == null || nums.length == 0) {
return ans;
}

//Arrays.sort(nums); // not necessary, just for unit test
dfs(nums, 0, new ArrayList<>(), ans);

return ans;
}

private void dfs(int[] nums, int pos, List<Integer> list, List<List<Integer>> ans) {

for (int i = pos; i < nums.length; i++) {
dfs(nums, i + 1, list, ans);
list.remove(list.size() - 1);
}
}


## 4. Summary

• Combinations: need to use pos, no for loop in the main function.
• Permutations: no need to use pos, use visited array to store which numbers are used, no for loop in the main function.
• Subsets: need to use pos, no for loop in the main function.