So, I've recently learned how to generate every single possible substring of a given length of a given string. Now, I'm trying to find every possible DISTINCT string of the given length. What I mean by that is that the string has all distinct letters. Order doesn't matter. So far, what I have is:
public static void distinctStrings(int maxLength, char [] alphabet, String news){
//converts char array to a string builder so you can mutate it
String a = new String (alphabet);
StringBuilder nAlphabet = new StringBuilder(a);
//if it is max
if(news.length()==maxLength) System.out.println(news);//full.add(news);
//describes the way to get the distinct string:
else{
//if alphabet.length>0 to avoid errors when the alphabet length has reached 0, probably could just do a while loop
if(alphabet.length>0){
for(int i = 0; i < alphabet.length; i++) {
String oldCurr = news;
news += nAlphabet.charAt(i);
//deletes char so it can't be used again
nAlphabet.deleteCharAt(i);
String c = nAlphabet.toString();
char[] b = c.toCharArray();
//reprocesses the strings.
distinctStrings(maxLength,b,news);
news = oldCurr;
}
}
}
EDIT: So the code isn't working, and I'm not sure why. It outputs "AB AB" and that's it. I ran it with distinctStrings(2, {'A', 'B', 'C'}, " "). I would also appreciate pointers in how to optimize it. The general idea of what I want to code to do is, if I plug in distinctStrings(2, {'A', 'B', 'C'}, " "), it is supposed to output AB, AC, BC. Order shouldn't matter. On the other hand, if I had wanted to output ALL possible strings, it would include strings like AA, BB, and CC, none of which I want. The term distinct strings means a string such that the characters included in it are all different. The reason I use the string "news" (which is just blank at the start) is that it's the starting point, and so that I can run the method in itself, and it runs the method on the new strings "news".