Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created January 19, 2015 22:53
Show Gist options
  • Save dmnugent80/c6912ace486cdc912c22 to your computer and use it in GitHub Desktop.
Save dmnugent80/c6912ace486cdc912c22 to your computer and use it in GitHub Desktop.
Combinations of a String
public class StringLib {
public static void combine(String str){
int length=str.length();
StringBuffer output=new StringBuffer();
combination(str,length,output,0);
}
static void combination(String str, int length, StringBuffer output, int level){
/* show arms-length recursion style with better peformance */
if (level==length)
return;
else{
for(int i=level;i<length;i++){
output.append(str.charAt(i));
System.out.println(output.toString());
//if (i<length-1)
combination(str,length,output,i+1);
output.deleteCharAt(output.length()-1);
}
}
}
public static void main(String[] args){
combine("abc");
}
}
/*Output:
a
ab
abc
ac
b
bc
c
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment