Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 23, 2017 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/2cdfab3561b631bc618533dc8128549d to your computer and use it in GitHub Desktop.
Save thmain/2cdfab3561b631bc618533dc8128549d to your computer and use it in GitHub Desktop.
public class PrintStringSubSequences {
public void printAllSubSequences(String input){
int [] temp = new int[input.length()];
int index = 0;
solve(input, index, temp);
}
private void solve(String input, int index, int [] temp){
if(index==input.length()){
print(input,temp);
return;
}
//set the current index bit and solve it recursively
temp[index] = 1;
solve(input,index+1,temp);
//unset the current index bit and solve it recursively
temp[index] = 0;
solve(input,index+1,temp);
}
private void print(String input, int [] temp){
String result = "";
for (int i = 0; i <temp.length ; i++) {
if(temp[i]==1)
result += input.charAt(i)+" ";
}
if(result=="")
result = "{Empty Set}";
System.out.println(result);
}
public static void main(String[] args) {
String input = "abc";
new PrintStringSubSequences().printAllSubSequences(input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment