Here’s a way:
import java.util.ArrayList;
/**
* Oops. Forgot to document this one.
*
* @author Kevin Glass
*/
public class Perms {
private int[] seq = new int[] {0,1,2,3,4};
private ArrayList matches = new ArrayList();
public Perms() {
find(0,1,2);
for (int i=0;i<matches.size();i++) {
System.out.println(matches.get(i));
}
}
public void find(int a,int b,int c) {
if ((a >= seq.length) || (b >= seq.length) || (c >= seq.length)) {
// invalid leg
return;
}
String match = seq[a]+""+seq[b]+""+seq[c];
if (matches.contains(match)) {
// already got this one, so don't bother to go any further
return;
}
matches.add(match);
if (c < seq.length-1) {
find(a,b,c+1);
}
if (b < seq.length-1) {
find(a,b+1,b+2);
}
if (a < seq.length-1) {
find(a+1,a+2,a+3);
}
}
public static void main(String[] argv) {
new Perms();
}
}
I’m sure there are better ways, far more elegant, this is just a first hack. However, its recursive and does find the sequence.
Kev