Create all permutations of 4 letters

2019-09-17 09:16发布

问题:

I want to create an array of all permutations of a series of 4 letters (Call them A,C,G,T for the 4 types of nucleotide bases). The program should ask the user for the value of k, the length of the permutation. I've got it working to the point where I can get the permutations, but it only shows the ones with no repeats. Here's the program, the output it's giving me right now, and the output I want it to give me.

import java.util.Arrays;
import TerminalIO.*;
public class Permute {
    static KeyboardReader reader= new KeyboardReader ();
    static int k= reader.readInt("Enter k-tuple");
    static void permute(int level, String permuted,
                        boolean[] used, String original) {

        if (level == k) {
            System.out.println(permuted);
        } else {
            for (int i = 0; i < 4; i++) {
                if (!used[i]) {
                    used[i] = true;
                    permute(level + 1, permuted + original.charAt(i), used, original);
                    used[i] = false;
                }
            }
        }
    }

    public static void main(String[] args) {
        String s = "ACGTACGTACGTACGTACGT";
        boolean used[] = new boolean[20];
        Arrays.fill(used, false);
        permute(0, "", used, s);
    }   
}

When I enter a K value of 2,it gives me:

  • AC
  • AG
  • AT
  • CA
  • CG
  • CT
  • GA
  • GC
  • GT
  • TA
  • TC
  • TG

Ideally, it would print:

  • AC
  • AG
  • AT
  • AA
  • CA
  • CC
  • CG
  • CT
  • GA
  • GG
  • GC
  • GT
  • TA
  • TC
  • TG
  • TT

回答1:

public class Permute {

    static String s = "ACGT";

    static void permute(int level, String prefix) {

        if (level == 0) {
            System.out.println(prefix);
            return;
        }
        for (int i = 0; i < s.length(); i++)
            permute(level - 1, prefix + s.charAt(i));
    }

    public static void main(String[] args) {
        int k = 4;
        permute(k, "");
    }   

}


回答2:

What about this? (without recursion )

void permute(char[] alphabet, int k) {
    int permutationNumber = (int) Math.pow(alphabet.length, k);
    for (int i = 0; i < permutationNumber; i++) {
       for (int j = 0; j < k; j++) {
          System.out.print(alphabet[(i + (j * i / alphabet.length)) % alphabet.length]);
       }
       System.out.println();
    }
 }


回答3:

ArrayList<ArrayList<String>> results= new ArrayList<ArrayList<String>>();

public static void main(String... args) {
    String[] letters= new String[] {"a", "t", "g", "c"};
    List<String> list= Arrays.asList(letters);

    for (int i=0; i<list.size(); i++) {
        ArrayList<String> startList= new ArrayList<String>();
        startList.add(list.get(i));
        permute(list, 2, startList);
    }

    //result lists of strings are saved in results
    for (ArrayList<String> result : results) {
        System.out.println(result);
    }
}

private static void permute(List<String> letters, int endLength, List<String> startList) {
    if (startList.size() >= endLength) {
        results.add(startList);
        return;
    }

    for (int i=0; i<letters.size(); i++) {
        ArrayList<String> newStartList= new ArrayList<String>(startList);
        newStartList.add(letters.get(i));
        permute(letters, 2, newStartList);
    }
}