Smart way to generate permutation and combination of String

前端 未结 7 458
耶瑟儿~
耶瑟儿~ 2021-02-01 22:37
String database[] = {\'a\', \'b\', \'c\'};

I would like to generate the following strings sequence, based on given database.



        
7条回答
  •  失恋的感觉
    2021-02-01 23:15

    For anyone looking for non-recursive options, here is a sample for numeric permutations (can easily be adapted to char. numberOfAgents is the number of columns and the set of numbers is 0 to numberOfActions:

        int numberOfAgents=5;
        int numberOfActions = 8;
        byte[][]combinations = new byte[(int)Math.pow(numberOfActions,numberOfAgents)][numberOfAgents];
    
        // do each column separately
        for (byte j = 0; j < numberOfAgents; j++) {
            // for this column, repeat each option in the set 'reps' times
            int reps = (int) Math.pow(numberOfActions, j);
    
            // for each column, repeat the whole set of options until we reach the end
            int counter=0;
            while(counter

提交回复
热议问题