Unscramble Letters in Java - Get All Possible Letter Combinations

Resolved: what I asked was resolved, but feel free to respond with alternative methods. here the unscrambler letter is made with the answer. Project page

I am currently a student at AP Computer Science. I am working on decrypting letters that reads in a dictionary and prints a list of words with an entered alphabetic set. To do this, I create a map with Map<String,Set<String>> in which the "earth" will be entered under the key "aerht" and in the corresponding set.

Example How Would I generate all of these:
CAKE -> ACEK
A          C           E           K
AC        CE           EK               
ACE       CEK            
ACEK

AE       CK
AEK
ACK
AK

The problem I am facing is that some key values ​​are not checked, as I am currently taking a set of numbers and alphabetical characters for example earth-> aehrt but this skips combos like aht-> hat or eht → the.

, , , . , → aehrt, a, ae, aeh, aehr, ah, ahr, ahrt, aer, aert , , , . [] a, e, h, r, t . , ArrayList of Set. "aehrt".

for(int z = 0; z<key.length();z++) {
    Set<String> temp = new HashSet<String>();

    //s1 = t*s0 ∪ {t} ∪ s0 = {t}
    for(String str: test.get(z)) //t*s0
                str+=letters[z];

    test.get(z).add(letters[z]); //{t}  
    test.get(z).addAll(test.get(z-1));//s0
    test.get(z).addAll(temp);
}
+4
3

, "aehrt", , :

  • :           S0 = {}
  • , a:   S1 = a⋅S0 ∪ S0 ∪ {a} = {a}
  • , e:   S2 = e⋅S1 ∪ S1 ∪ {e} = {ae, a, e}
  • , h:   S3 = h⋅S2 ∪ S2 ∪ {h} = {aeh, ah, eh, ae, a, e, h}
  • ....

, S5 ( ), .


public static void main(String... args){     
    Set<String> set = new TreeSet<String>();
    String key = "aehrt";

    //S1 = c*S0 ∪ {c} ∪ S0
    for(int z = 0; z < key.length();z++) {
        Set<String> temp = new HashSet<String>();
        char c = key.charAt(z);        

        for(String str: set)
            temp.add(str + c); // ∪ c*S0
        set.add(c+"");         // ∪ {c}
        set.addAll(temp);      // ∪ S0
    }

    System.out.println(set);
}

output: [a, ae, aeh, aehr, aehrt, aeht, aer, aert, aet, ah, ahr, ahrt, aht, ar, art,
         at, e, eh, ehr, ehrt, eht, er, ert, et, h, hr, hrt, ht, r, rt, t]
+3

, String CAKE: 4 . 4C1 + 4C2 + 4C3 + 4C4 = 2^4 - 1 = 15

C A K E CA Ak KE EC CK CE CAK AKE KEC CKE CAKE.

1 2 ^ 4-1, 0001 0010 0011 0100 .   String CAKE. 0, .

0001 = _ _ _ E

0010 = _ _ K _

0011 = _ _ KE

0100 = _ A _ _

.. . , java:

public class AllCombinations {
    public static void main(String[] args) {
        char c[] = new char[] {'C','A','K','E'};
        int t = (int) Math.pow(2, c.length);
        for(int i=1;i<t;i++) {
            String s = Integer.toBinaryString(i);
            String comb = getComb(s,c);
            System.out.println(comb);
        }
    }

    private static String getComb(String s, char[] c) {
        String comb = "";
        int len = s.length();
        for(int i=0;i<s.length();i++) {
            if(s.charAt(i) == '1') {
                comb += c[len-i-1];
            }
        }
        return comb;
    }
}
+1

, . , "" 5- , 1 31. 0 "e" "h"; . .

. . (, HashSet -, ... , 2 "e" - .) , "" 1 "e" , 2 "a", 3 "ea", 4 "r" ..

(1 < ), .

: /, . , , , .

0

All Articles