Untitled
unknown
java
a year ago
1.2 kB
11
Indexable
public class Main {
public static void main(String[] args) {
String str = "100?10?1010?000";
System.out.println("All possible combinations:");
generateCombinations(str.toCharArray(), 0); // Start at the first index
}
// Recursive function to generate combinations
public static void generateCombinations(char[] str, int index) {
// Base case: If we reach the end of the string, print the result
if (index == str.length) {
System.out.println(new String(str)); // Print the current combination
return;
}
// If the current character is '?', replace it with '0' and '1'
if (str[index] == '?') {
// Replace with '0' and recurse
str[index] = '0';
generateCombinations(str, index + 1);
// Replace with '1' and recurse
str[index] = '1';
generateCombinations(str, index + 1);
// Backtrack: Set it back to '?' to explore other branches
str[index] = '?';
} else {
// If the current character is not '?', just move to the next character
generateCombinations(str, index + 1);
}
}
}Editor is loading...
Leave a Comment