-3

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".

user4500882
  • 303
  • 3
  • 8

1 Answers1

0

Is this what you intend?

public static void distinctStrings(int maxLength, char[] alphabet, String news) {
    // a set that will enforce only distinct words
    Set<String> wordsDistinct = new HashSet<String>();

    // find all distinct words, of length maxLength
    for (int i = 0; i < news.length() - maxLength; ++i) {
        // possible a valid word
        String word = news.substring(i, i + maxLength);

        // validation test
        boolean isValid = true;
        for (char c: alphabet) {
            if (word.contains(String.valueOf(c))) {
                isValid = false;
                break;
            }
        }
        if (!isValid)
            continue; // probably not valid, because of the alphabet, or maybe is vice-versa 

        // add the word to set. If already there ... the set will ignore it.
        wordsDistinct.add(word);
    }

    // print the strings
    for (String s : wordsDistinct) {
        System.out.println(s);
    }
}

I run it with:

    DistinctStrings ds = new DistinctStrings();
    char a[] = {' ', ';'};
    ds.distinctStrings(4, a, "lorem ipsum dolor sit amet; lorem ipsum");

Output:

psum
dolo
olor
amet
ipsu
orem
lore
azbarcea
  • 3,323
  • 1
  • 20
  • 25
  • No, I don't think you quite understand. I intend for my code, such that given a string, prints all possible distinct substrings of the characters. I will edit the question in order to make it more clear – user4500882 Feb 06 '15 at 11:29
  • suct that given a char array, sorry. In your case, the char array is only "a", so that the output should be: "lorem ipsum dolor sit amet; lorem ipsum" with a space at the end, with a semicolon, and with a space then a semicolon because it simply takes the initial string, and adds all possible strings with distinct characters (taken from the char array) which in your case is simply the space and semicolon. – user4500882 Feb 06 '15 at 11:45