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