Skip to content

Commit 77f1345

Browse files
committed
Refactor: Cleanup for Combination.java
Just changed class name, variable names and comments to make it more clear and understandable.
1 parent ebed8b3 commit 77f1345

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/main/java/com/thealgorithms/backtracking/Combination.java renamed to src/main/java/com/thealgorithms/backtracking/CombinationGenerator.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,43 @@
55
import java.util.List;
66
import java.util.TreeSet;
77

8+
89
/**
9-
* Finds all permutations of given array
10+
* Finds all combinations of a given array that are a specific length by using backtracking.
1011
* @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
1112
*/
12-
public final class Combination {
13-
private Combination() {
13+
public final class CombinationGenerator {
14+
// Default private constructor to prevent instantiation
15+
16+
private CombinationGenerator() {
1417
}
1518

16-
private static int length;
19+
private static int length; //Define length outside all methods so that it can be accessed anywhere in the class.
20+
1721

1822
/**
1923
* Find all combinations of given array using backtracking
20-
* @param arr the array.
21-
* @param n length of combination
24+
* @param arr the array we want to find combinations of.
25+
* @param desiredLength the length of our desired combinations.
2226
* @param <T> the type of elements in the array.
23-
* @return a list of all combinations of length n. If n == 0, return null.
27+
* @return a list of all combinations of length desiredLength. If desiredLength is 0, return null.
2428
*/
25-
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
26-
if (n == 0) {
29+
public static <T> List<TreeSet<T>> combination(T[] arr, int desiredLength) {
30+
if (desiredLength == 0) {
2731
return null;
2832
}
29-
length = n;
33+
length = desiredLength;
3034
T[] array = arr.clone();
3135
Arrays.sort(array);
3236
List<TreeSet<T>> result = new LinkedList<>();
3337
backtracking(array, 0, new TreeSet<T>(), result);
3438
return result;
3539
}
3640

41+
3742
/**
3843
* Backtrack all possible combinations of a given array
39-
* @param arr the array.
44+
* @param arr the array from earlier.
4045
* @param index the starting index.
4146
* @param currSet set that tracks current combination
4247
* @param result the list contains all combination.

0 commit comments

Comments
 (0)