9

I want to create all possible 5 letter words using a-z.Please suggest any good and fast algorithms.

I have tried creating one and it looks something like this...

     byte[] allchar=new byte[] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
 int lengthOfAllChar=allchar.length;
         System.out.println(lengthOfAllChar);
        for (int i = 0; i < lengthOfAllChar; i++){
            for(int j = 0; i < lengthOfAllChar; j++){
                StringBuffer finalWordBuffer = new StringBuffer();
                finalWordBuffer.append((char)allchar[i]);
                finalWordBuffer.append((char)allchar[j]);
            }
        }
AutoMEta
  • 1,339
  • 6
  • 22
  • 35
  • 2
    we're talking 12M words. Good luck with that fast algorithm :) – iluxa Mar 31 '11 at 17:43
  • No i have created one.and it looks something like this... for(int i=0;i – AutoMEta Mar 31 '11 at 17:44
  • 1
    The Big O notation is (O^5), and in your case = 11881376 words. Good luck! – Buhake Sindi Mar 31 '11 at 17:45
  • 1
    Some context might help. What language (not the programming language!)? Why are you generating them, instead of looking them up from a dictionary? Is this homework, or an assignment of some sort? – Bart Kiers Mar 31 '11 at 17:45
  • what are the rules for your words (e.g. no more than two consecutive vowels)? I hope that there are some, because otherwise, this is a very easy problem to solve. – vlad Mar 31 '11 at 17:46
  • @AutoMEta, re-read my comment again please. I specifically didn't ask for the programming language since that information is already in the tags of your "question". Also please answer **all** questions. Thanks. – Bart Kiers Mar 31 '11 at 17:47
  • 2
    @AutoMeta: Edit your question and add the code there instead of posting as a comment. Don't forget to *mark* it as code. – MAK Mar 31 '11 at 17:47
  • Rules will be added after i have a fast and reliable algorithm... – AutoMEta Mar 31 '11 at 17:47
  • How fast does it need to be? I just wrote a test program do do it here, and it generates every permutation (not actual words) and writes them to a file in 1.6 seconds on my machine. I'm not sure all these comments that it will be very slow are that accurate. – Carl Norum Mar 31 '11 at 17:48
  • @carl..Can you post it in the answers... – AutoMEta Mar 31 '11 at 17:52
  • @AutoMEta, I wrote it in C, so I don't think it would help you. I can post it if you want, though. – Carl Norum Mar 31 '11 at 18:07
  • if its different form what i am using..u r welcome – AutoMEta Mar 31 '11 at 18:38
  • 1
    -1 voters, please explain what is wrong with this question, I feel it's completely valid question, we are not here to solve stupid syntax errors only. – Akash Kava Mar 31 '11 at 19:23

5 Answers5

26

Here's an example of generating all sequences for any set of characters at any length:

public class WordPermutations {
    public static void main(String[] args) {
        char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        int len = 5;
        iterate(chars, len, new char[len], 0);
    }

    public static void iterate(char[] chars, int len, char[] build, int pos) {
        if (pos == len) {
            String word = new String(build);
            // do what you need with each word here
            return;
        }

        for (int i = 0; i < chars.length; i++) {
            build[pos] = chars[i];
            iterate(chars, len, build, pos + 1);
        }
    }
}

This takes about 250ms on my machine to iterate through all 11,881,376 sequences.

Note that a new char[len] is only created once at the beginning and reused as build for building the permutations. The first call to iterate() starts with a pos of 0. Skip down to the for loop where it loops through each of chars. The first char of build is set to that and then we recursively call the same method to set the next one at pos + 1. Once this has happened 5 times the pos will be at len. This is when the pos == len kicks in at the top of the method. Then it just builds a String from what's built up in build and there's your word.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • 1
    Tats cool!!!!This seems a little tricky...Will you explain a little on how this works..It will be really helpful. – AutoMEta Mar 31 '11 at 18:49
  • 2
    Sure, I'll explain. Note that a `new char[len]` is only created once at the beginning and reused as `build` for building the permutations. The first call to `iterate()` starts with a `pos` of 0. Skip down to the `for` loop where it loops through each of `chars`. The first `char` of `build` is set to that and then we recursively call the same method to set the next one at `pos + 1`. Once this has happened 5 times the `pos` will be at `len`. This is when the `pos == len` kicks in at the top of the method. Then it just builds a `String` from what's built up in `build` and there's your word. – WhiteFang34 Mar 31 '11 at 20:07
  • Just correction... they are not permutations, but just sequences of length 5. When you have 26 characters, a 5-letter word can't be a "permutation" of them. – Antti Huima Apr 01 '11 at 22:45
  • 1
    i like how you used a method named "iterate" to recurse... ;) – jtahlborn Apr 01 '11 at 23:24
  • @antti: oops you're right, renamed permutations to sequences. – WhiteFang34 Apr 02 '11 at 00:07
  • @jtahlborn: hehe, I'm going to leave that :) – WhiteFang34 Apr 02 '11 at 00:11
  • Just curious, does anyone know if the big o on this algorithm is 2^n? – VirtualProdigy Nov 30 '18 at 04:14
5

This can be done easily also without recursion (here in C)

int i, k, n;
char tmp[6]; tmp[5] = 0;
for (i=0;i<26*26*26*26*26;i++) {
   n = i;
   for (k=4;k>=0;k--){
      tmp[k] = 'a' + (n % 26); 
      n /= 26;
   }
   output_string(tmp);
}

or you can do it with carry:

char tmp[6]; int i, k;
strcpy(tmp, "aaaaa");
for (i=0;i<26*26*26*26*26;i++) {
   output_string(tmp);
   tmp[4]++;
   k = 4;
   while (k > 0 && tmp[k] == 'z') { tmp[k] = 'a'; k--; tmp[k]++; }
}
Community
  • 1
  • 1
Antti Huima
  • 25,136
  • 3
  • 52
  • 71
3
public static List<String> getAll(int length) {
    final char[] chars = "0123456789".toCharArray();
    final double NUMBER_OF_PERMUTATIONS = Math.pow(chars.length, length);

    List<String> words = new ArrayList<>(Double.valueOf(
            NUMBER_OF_PERMUTATIONS).intValue());

    char[] temp = new char[length];
    Arrays.fill(temp, '0');

    for (int i = 0; i < NUMBER_OF_PERMUTATIONS; i++) {
        int n = i;
        for (int k = 0; k < length; k++) {
            temp[k] = chars[n % chars.length];
            n /= chars.length;
        }
        words.add(String.valueOf(temp));
    }
    return words;
}  

Here is the Java 7 version of antti.huima's code.

David M. Coe
  • 309
  • 1
  • 6
  • 14
0

Here's an algorithm for you to try out, in pseudocode:

array seenWords;
while size of seenWords[] < 26^5:
  generate random string of length 5 letters
  is string in seenWords?
  yes:
    go back to while
  no:
    push string onto end of seenWords[]
done while

You should be able to easily translate this pseudocode into proper Java code. The only tricky bit is generating the random string. You could take your array of letters, pick a random value between 1 and 26, then use that for a letter. Repeat that five times and you have a five-letter string!

Whether or not this is a "good" or "fast" algorithm is up to you. You haven't defined what "good" or "fast" mean, so I'm unable to judge. You may have different criteria than I do.

Note that this will generate all strings that have five letters in them. These will probably not be words. Judging from your sample code you want all strings that have five letters in them, not words that have five letters in them.

CanSpice
  • 34,814
  • 10
  • 72
  • 86
  • 5
    why complicate things with generating random numbers? five nested `for` loops guarantee that all 'words' are created, without duplicates. – vlad Mar 31 '11 at 18:06
  • is string in seenWords? This becomes difficult...you have to match each generated word.. – AutoMEta Mar 31 '11 at 18:08
  • @vlad: "whether or not this is a 'good' or 'fast' algorithm is up to you." :-) There are obvious ways of solving this problem, I gave a non-obvious one. – CanSpice Mar 31 '11 at 18:09
0
public void wordCreator(int length){
    Random rnd=new Random();
    String word;
    do{
        word="";
        for(int j=0; j<length; j++){
            int index=rnd.nextInt(data.length);   //data is a String array letter of the alpabet
            word+=data[index];
        }
    }while(wordMap.containsValue(word));
    wordMap.put(i, word);
    i++;

}

Here is your algorithm

v1ewb0x
  • 3
  • 3