All possible words

I want to create all possible 5 alphabetic words using az. Please suggest any good and fast algorithms.

I tried to create 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]); } } 
+8
java algorithm logic
source share
4 answers

Here is an example of generating all sequences for any character set of 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); } } } 

It takes about 250 ms on my machine to repeat all 11,881,376 sequences.

Note that the new char[len] is created only once at the beginning and is reused as an assembly to build permutations. The first call to iterate() starts with pos from 0 . Go to the for loop, where it goes through each of the characters. The first char of the assembly is set to this, and then we recursively call the same method to set the next to pos + 1 . Once this happened 5 times, pos will be in len . This is when pos == len is at the top of the method. Then it just builds a String from what is created in the assembly, and there your word is.

+20
source share

This can be done easily and 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 a 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]++; } } 
+3
source share
 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 antti.huima code version in Java 7.

+2
source share

Here is the algorithm for you to try, 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 the correct Java code. The only hard bit is random string generation. You can take your array of letters, choose a random value from 1 to 26, and then use this for writing. Repeat this five times and you have a five letter string!

Whether it is a β€œgood” or β€œfast” algorithm is up to you. You have not defined what β€œgood” or β€œfast” means, so I cannot judge. You may have different criteria than me.

Note that this will create all lines that have five letters. These are probably not words. Judging by your sample code, you want all lines to have five letters in them, and not words that have five letters.

0
source share

All Articles