Created
April 8, 2023 17:32
-
-
Save HamsterofDeath/a711b2adf3258f30dbe06e77c0bcd09b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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