package recursion;

//Example of a "Backtracking Search" solution to a problem

public class RecursiveWordSearch {

    private static String pool;
    private static boolean[] used;

    private static void printWords(String word) {

        if (word.length() > 0) {
            System.out.println(word);
        }

        if (word.length() == pool.length()) {
            return;
        }

        // append each unused char to the current string
        for (int i = 0; i < pool.length(); ++i) {
            if (!used[i]) {
                used[i] = true;
                printWords(word + Character.toString(pool.charAt(i)));
                used[i] = false;
            }
        }
    }

    public static void main(String[] args) {

        pool = "toothpaste";//args[0];
        used = new boolean[pool.length()];

        printWords("tooth");
    }

}
