// 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 WGenFlex {
    public interface WGenProcesser {
        public void process(String w);
    }
    private static void gen(char[] c, int ix, int n, char[] base, WGenProcesser p) {
        if(ix < n) {
            for(int i = 0; i < base.length; i++) {
                c[ix] = base[i];
                gen(c, ix+1, n, base, p);
            }
        } else {
            p.process(new String(c));
        }
    }
    private static void gen(int n, char[] base, WGenProcesser p) {
        char[] c = new char[n];
        gen(c, 0, n, base, p);
    }
    public static void gen(int n1, int n2, char[] base, WGenProcesser p) {
        for(int i = n1; i <= n2; i++) {
            gen(i, base, p);
        }
    }
    public static void main(String[] args) {
        gen(1, 2, "abcdefghijklmnopqrstuvwxyz".toCharArray(),
            new WGenProcesser() {
                public void process(String s) {
                    System.out.println(s);
                }
            });
    }
}
