所有可能的词

发布于 2024-10-29 05:49:11 字数 631 浏览 5 评论 0原文

我想使用AZ创建所有可能的5个字母单词。请建议任何好的快速算法。

我尝试过创建一个,看起来像这样...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Smile简单爱 2024-11-05 05:49:11

下面是为任意长度的任意字符集生成所有序列的示例:

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

在我的机器上,这大约需要 250 毫秒来迭代所有 11,881,376 个序列。

请注意,新的 char[len] 在开始时仅创建一次,并重用为构建排列的构建。第一次调用 iterate() 时,pos0。跳到 for 循环,循环遍历每个字符。 build 的第一个字符设置为该值,然后我们递归调用相同的方法将下一个字符设置为 pos + 1。一旦这种情况发生 5 次,pos 将位于 len。这时,pos == len 在方法的顶部启动。然后它只是根据 build 中构建的内容构建一个 String ,然后就可以了。

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.

负佳期 2024-11-05 05:49:11

这也可以轻松完成,无需递归(此处在 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);
}

,或者您可以使用进位来完成:

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

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]++; }
}
两个我 2024-11-05 05:49:11
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;
}  

这是 antti.huima 代码的 Java 7 版本。

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.

乞讨 2024-11-05 05:49:11

下面是一个供您尝试的伪代码算法:

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

您应该能够轻松地将这个伪代码转换为正确的 Java 代码。唯一棘手的一点是生成随机字符串。您可以获取字母数组,选择 1 到 26 之间的随机值,然后将其用作字母。重复五次,你就得到了一个由五个字母组成的字符串!

这是一个“好”还是“快”的算法取决于你。你还没有定义什么是“好”或“快”,所以我无法判断。你的标准可能和我不同。

请注意,这将生成所有包含五个字母的字符串。这些可能不会是言语。从示例代码来看,您需要所有包含五个字母的字符串,而不是包含五个字母的单词。

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.

忘东忘西忘不掉你 2024-11-05 05:49:11
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++;

这是你的算法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文