所有可能的话的话

2023-09-11 02:23:59 作者:語氚柒分熟

我想用az.Please提出任何又好又快的算法来创造一切可能的5字母的单词。

我曾尝试创建一个,它看起来是这样的...

 字节[] allchar =新的字节[] {'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;我++){
            对于(INT J = 0; I< lengthOfAllChar; J ++){
                StringBuffer的finalWordBuffer =新的StringBuffer();
                finalWordBuffer.append((炭)allchar [I]);
                finalWordBuffer.append((炭)allchar [J]);
            }
        }
 

解决方案

下面是生成的所有序列的任何字符集的任何长度的例子:

 公共类WordPermutations {
    公共静态无效的主要(字串[] args){
        的char []字符=ABCDEFGHIJKLMNOPQRSTUVWXYZ.toCharArray();
        INT LEN = 5;
        迭代(字符,len个,新的char [LEN],0);
    }

    公共静态无效的迭代(的char []字符,INT LEN,CHAR []打造,INT POS){
        如果(POS == LEN){
            串字=新的String(建设);
            //做什么,你需要在这里的每个字
            返回;
        }

        的for(int i = 0; I< chars.length;我++){
            建[POS] =字符[I]
            迭代(字符,len个,建设,POS + 1);
        }
    }
}
 
不建议家长对孩子说的话,可能不利于孩子成长

这大约需要250毫秒我的机器上遍历所有的11881376序列。

请注意,一个新的的char [LEN] 仅在创建之初曾经和再利用作为构建用于构建排列。第一次调用迭代()开始以 POS 0 。跳转到for循环它遍历每个字符的。构建的第一个字符被设置为,然后我们递归调用相同的方法来设置下一个在 POS + 1 。一旦发生这种情况的5倍POS将在 len个。这是当 POS == LEN 踢在该方法的顶部。然后,它只是建立了一个字符串从什么建立在建和有你的话。

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]);
            }
        }

解决方案

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.