java - 生成字符串排列组合的智能方式

标签 java algorithm combinations permutation

String database[] = {'a', 'b', 'c'};

我想根据给定的数据库生成以下字符串序列。

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
...

我只能想到一个漂亮的“虚拟”解决方案。

public class JavaApplication21 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        char[] database = {'a', 'b', 'c'};

        String query = "a";
        StringBuilder query_sb = new StringBuilder(query);
        for (int a = 0; a < database.length; a++) {
            query_sb.setCharAt(0, database[a]);
            query = query_sb.toString();                    
            System.out.println(query);            
        }

        query = "aa";
        query_sb = new StringBuilder(query);
        for (int a = 0; a < database.length; a++) {
            query_sb.setCharAt(0, database[a]);    
            for (int b = 0; b < database.length; b++) {    
                query_sb.setCharAt(1, database[b]);    
                query = query_sb.toString();                    
                System.out.println(query);
            }
        }

        query = "aaa";
        query_sb = new StringBuilder(query);
        for (int a = 0; a < database.length; a++) {
            query_sb.setCharAt(0, database[a]);    
            for (int b = 0; b < database.length; b++) {    
                query_sb.setCharAt(1, database[b]);    
                for (int c = 0; c < database.length; c++) {                    
                    query_sb.setCharAt(2, database[c]);                        
                    query = query_sb.toString();                    
                    System.out.println(query);
                }
            }
        }
    }
}

解决方案非常愚蠢。从某种意义上说,它是不可扩展的

  1. 如果我增加数据库的大小会怎样?
  2. 如果我的最终目标打印字符串长度需要为 N 怎么办?

是否有任何智能代码,可以以非常智能的方式生成可扩展的排列和组合字符串?

最佳答案

你应该检查这个答案:Getting every possible permutation of a string or combination including repeated characters in Java

获取此代码:

public static String[] getAllLists(String[] elements, int lengthOfList)
{

    //lists of length 1 are just the original elements
    if(lengthOfList == 1) return elements; 
    else {
        //initialize our returned list with the number of elements calculated above
        String[] allLists = new String[(int)Math.pow(elements.length, lengthOfList)];

        //the recursion--get all lists of length 3, length 2, all the way up to 1
        String[] allSublists = getAllLists(elements, lengthOfList - 1);

        //append the sublists to each element
        int arrayIndex = 0;

        for(int i = 0; i < elements.length; i++){
            for(int j = 0; j < allSublists.length; j++){
                //add the newly appended combination to the list
                allLists[arrayIndex] = elements[i] + allSublists[j];
                arrayIndex++;
            }
        }
        return allLists;
    }
}

public static void main(String[] args){
    String[] database = {"a","b","c"};
    for(int i=1; i<=database.length; i++){
        String[] result = getAllLists(database, i);
        for(int j=0; j<result.length; j++){
            System.out.println(result[j]);
        }
    }
}

虽然可以进一步改进内存,因为这个解决方案首先生成内存(数组)的所有解决方案,然后我们才能打印它。但是思路是一样的,就是用递归算法。

关于java - 生成字符串排列组合的智能方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993073/

相关文章:

java - LinkedList addAfter方法不会更新下一个节点的前一个成员变量

python - 密码战。一些测试通过了,但我需要得到输出以下错误的测试 : 3263 should equal -1

c# - 如何实现序列的约束改组

r - 通过替换具有多种可能性的字符来生成所有组合的列表

java - 将空值的两倍转换为字符串错误

java - 在由 Maven 驱动的简单演示应用程序的 JAR 中包含 Postgres 的 JDBC 驱动程序

java - 安卓文件关联

python - 如何快速创建不替换总体的随机样本?

python - 计算包含给定单词一次的固定长度字符串的数量

arrays - 在Perl中,如何生成列表的所有可能组合?