// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class Perm2 {
    public static void writePerm(char[] a, String prefix, int ix, int len, int[] avail) {
        if(ix < len) {
            for(int i = 0; i < a.length; i++) {
                if(avail[i] > 0) {
                    avail[i]--;
                    writePerm(a, prefix + a[i], ix + 1, len, avail);
                    avail[i]++;
                }
            }
        } else {
            System.out.println(prefix);
        }
    }
    public static void writePerm(char[] a, int[] avail, int len) {
        writePerm(a, "", 0, len, avail);
    }
    public static void main(String[] args) {
        writePerm(new char[] { '0', '2', '5', '6', '8' }, new int[] { 1, 2, 1, 1, 1}, 3);
    }
}
