Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

I'm not sure why you would want to do this in the first place.

The resulting set for any moderately large values of x and y will be huge, and will grow exponent ially as x and/or y get bigger. Lets say your set of possible characters is the 26 lowercase letters of the alph abet, and you ask your application to generate all permutations where length = 5 . Assuming you don't run out of memory you'll get 11,881,376 (i.e. 26 to the pow er of 5) strings back. Bump that length up to 6, and you'll get 308,915,776 stri ngs back. These numbers get painfully large, very quickly. Here's a solution I put together in Java. You'll need to provide two runtime arg uments (corresponding to x and y). Have fun. public class GeneratePermutations { public static void main(String[] args) { int lower = Integer.parseInt(args[0]); int upper = Integer.parseInt(args[1]); if (upper < lower || upper == 0 || lower == 0) { System.exit(0); } for (int length = lower; length <= upper; length++) { generate(length, ""); } } private static void generate(int length, String partial) { if (length <= 0) { System.out.println(partial); } else { for (char c = 'a'; c <= 'z'; c++) { generate(length - 1, partial + c); } } } }

You might also like