Skip to content

Instantly share code, notes, and snippets.

@HamsterofDeath
Created April 8, 2023 17:32
Show Gist options
  • Save HamsterofDeath/a711b2adf3258f30dbe06e77c0bcd09b to your computer and use it in GitHub Desktop.
Save HamsterofDeath/a711b2adf3258f30dbe06e77c0bcd09b to your computer and use it in GitHub Desktop.
package fun;
public class SubsetsGenerator {
public static void main(String[] args) {
String input = "cat";
for (int i = 0; i <= input.length(); i++) {
generateCombinations(input, "", 0, i);
}
}
/**
* Generates all combinations of the input string of a specific length using recursion.
*
* @param input The input string for which combinations are to be generated.
* @param current The current combination being built.
* @param index The current index in the input string.
* @param length The desired length of the combinations.
*/
public static void generateCombinations(String input, String current, int index, int length) {
if (current.length() == length) {
System.out.println(current);
return;
}
if (index == input.length()) {
return;
}
// Exclude the current character
generateCombinations(input, current, index + 1, length);
// Include the current character
generateCombinations(input, current + input.charAt(index), index + 1, length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment