Find all possible subsets given by a string

I am trying to find all possible string anagrams in Java. By this I mean that if I have a long word with a length of 4 characters, I want all possible 3 character long words to be derived from it, all two characters long and all 1 character long. The easiest way is to use two nested loops and iterate over the line. This is my code at the moment:

private ArrayList<String> subsets(String word){
        ArrayList<String> s = new ArrayList<String>();
        int length = word.length();
        for (int c=0; c<length; c++){
            for (int i=0; i<length-c; i++){
                String sub = word.substring(c, c+i+1);
                System.out.println(sub);
                //if (!s.contains(sub) && sub!=null) 
                    s.add(sub);
            }
        }
        //java.util.Collections.sort(s, new MyComparator());
        //System.out.println(s.toString());
        return s;
    }

My problem is that it works for three letter words, funsets this result (do not pay attention to ordering, the word is processed so that I have a line with letters in alphabetical order):

f
fn
fnu
n
nu
u

But when I try 4 letters, it leaves something, as in catqgives me:

a
ac
acq
acqt
c
cq
cqt
q
qt
t

i.e. 3 act - . , , , , , . - , , , . , .

: -, acq, qca, caq, aqc, cqa, qac .. - . , , , , acq. , , , , 4 , 3- , , - , . , .

+4
4

, , ,

private void subsets(String word, ArrayList<String> subset){
        if(word.length() == 1){
            subset.add(word);
            return;
        } 
        else {
            String firstChar = word.substring(0,1);
            word = word.substring(1);
            subsets(word, subset);
            int size = subset.size();
            for (int i = 0; i < size; i++){
                String temp = firstChar + subset.get(i);
                subset.add(temp);
            }
            subset.add(firstChar);
            return;
        }
    }

, , , , ArrayList . , String. , , , , , , 1, .

, , , , , - 1, for , , ArrayList. . I.E., fun :

f saved
List empty
recursive call(un)
-
u saved
List empty
recursive call(n)
-
n.length == 1
List = [n]
return
-
list.size=1
temp = u + list[0]
List = [n, un]
add the character saved in the stack on its own
List = [n, un, u]
return
-
list.size=3
temp = f + list[0]
List = [n, un, u, fn]
temp = f + list[1]
List = [n, un, u, fn, fun]
temp = f + list[2]
List = [n, un, u, fn, fun, fu]
add the character saved in the stack on its own
List = [n, un, u, fn, fun, fu, f]
return

, , , , .

0

, "caqt" "acqt" /.

(, , . , , .)

: . .

+1

, , . -, , . . 2 , 2 ^ n , n - . , , , .

, 0 "include this letter", 1 , "fnu", :

000 - ''
001 - 'u'
010 - 'n'
011 - 'nu'
100 - 'f'
101 - 'fu' (no offense intended)
110 - 'fn'
111 - 'fnu'.

, , , 0-7 , .

java.. java-, :

public string getSubSet(string input, int index) {
  // Should check that index >=0 and < 2^input.length here.
  // Should also check that input.length <= 31.
  string returnValue = "";
  for (int i = 0; i < input.length; i++) {
    if (i & (1 << i) != 0) // 1 << i is the equivalent of 2^i
      returnValue += input[i];
  }
  return returnValue;
}

, , , , :

for (i = 1; i < (1 << input.length); i++)
  getSubSet(input, i); // this doesn't do anything, but you can add it to a list, or output it as desired.

. 1 0 - , 0 . , , "f", "n", "fn", "u", "fu", "nu", "fnu", didn ' t .

+1

:

public static void main(String[] args) {
    String input = "abcde";
    Set<String> returnList = permutations(input);
    System.out.println(returnList);
}

private static Set<String> permutations(String input) {
    if (input.length() == 1) {
        Set<String> a = new TreeSet<>();
        a.add(input);
        return a;
    }
    Set<String> returnSet = new TreeSet<>();

    for (int i = 0; i < input.length(); i++) {
        String prefix = input.substring(i, i + 1);
        Set<String> permutations = permutations(input.substring(i + 1));
        returnSet.add(prefix);
        returnSet.addAll(permutations);
        Iterator<String> it = permutations.iterator();
        while (it.hasNext()) {
            returnSet.add(prefix + it.next());
        }
    }
    return returnSet;
}
0

All Articles