Created
January 19, 2015 22:53
-
-
Save dmnugent80/c6912ace486cdc912c22 to your computer and use it in GitHub Desktop.
Combinations of a String
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
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