I would be very tempted to see if the generation of possibilites can be done recursively -- and generated in a way, that checks for duplicates as part of the process. It will likely not be faster, but at least it won't have the ridiculous number of nested "for" loops, and nested "if" conditions.... Okay, and the other reason would be just to see if it can be done that way...
Henry