// File:  Perms.java
//
// A highly recursive way to print the permutations of a string

public class Perms {

    public Perms() { }

    public Perms(String startPerms) {
        System.out.println("Initial string to be permuted: " + startPerms);
        nNumPerms = 0;
        makePerms("", startPerms);
        System.out.println("There were " + nNumPerms + " permutations");
        System.out.println();
    }

    private void makePerms(String str1, String str2) {
        int i = str2.length() - 1;
        if (i < 1) {
            System.out.println(str1+str2);
            ++nNumPerms;
        } else
            for (int j = 0; j <= i; j++)
                makePerms(str1 + str2.substring(j, j+1),
                          str2.substring(0, j) + str2.substring(str2.length() - (i - j)));
    }

    public static void main(String args[]) {
        String startPerms = "";
        for (int i = 1; i < 7; i++) {
            startPerms += i;
            new Perms(startPerms);
        }
    }

    private static int nNumPerms;

}
