Question

I'd like to generate all possible strings, from length 1 to length 5, consisting of the characters a-z and 0-9.

I've found snippets of code that can probably help me, but I am stuck on modifying them to achieve what I need.

This bit of code I found on SO will produce all possible string combinations of length 4 using a-z and 0-9, 1679616 strings.

public static void generate(){
    int MAX = 36;
    long count = 1L * MAX * MAX * MAX * MAX;

    int counter = 0;
    char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
    int[] digits = new int[4];
    char[] output = "0000".toCharArray();

        for (int i= 0; i < count; i++){
            String name = String.valueOf(output);

            for(int d =  3; d >=0; --d){
                digits[d] = (digits[d] + 1) % MAX;
                output[d] = alphabet[digits[d]];
                if (digits[d] > 0){
                    break;
                }
            }
            counter++;
            System.out.println(name);
        }
    System.out.println(counter);
}

Now that's not what I need exactly, but it's close and maybe I can make it work! I've tried modifying in it the following way but there are index issues that are throwing ArrayOutBounds exceptions everywhere. The thought process is, I can just wrap this entire block in another for loop that deals with the size of digits and output, which would go from 1 to 5. The more time I spend modifying, the uglier it gets and the more I am thinking, "there must be a cleaner way".

public static void generate(){
    int MAX = 36;
    long count = 1L * MAX * MAX * MAX * MAX * MAX;

    String build = null;
    int counter = 0;
    char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
    int[] digits = null;
    char[] output = null;

    for (int j = 1; j < 5; j++){
      build = null; //clear build
      digits = null; //clear digits
      outputs = null; //clear outputs
      digits = new int[j]; //initialize digits to size j

      //small loop to append "0" to build 
      for (int m = 0; m < j; m++){
          build = build + "0";
      }
      output = build.toCharArray(); //convert our string to charArray

        for (int i= 0; i < count; i++){
            String name = String.valueOf(output);

            for(int d =  3; d >=0; --d){
                digits[d] = (digits[d] + 1) % MAX;
                output[d] = alphabet[digits[d]];
                if (digits[d] > 0){
                    break;
                }
            }
            counter++;
            System.out.println(name);
        }
    }
    System.out.println(counter);
}
Was it helpful?

Solution

You need to modify the lines

    long count = 1L * MAX * MAX * MAX * MAX * MAX; // one more * MAX

    ...

    int[] digits = new int[5]; // from [4]
    char[] output = "00000".toCharArray(); // from 0000

    ...

            for(int d =  4; d >=0; --d){ // from d = 3

More generally, if n is the number of digits,

    long count = 1L;
    for( int i=0; i<n; i++ ) count *= MAX;

    ...

    int[] digits = new int[n];
    char[] output = new char[n];
    Arrays.fill( output, '0' );

    ...

            for(int d = n-1; d >=0; --d){

OTHER TIPS

You could modify generate to take a leading character as a parameter and print all 5-character strings that start with that character. Then call this modified generate with each of the possible leading characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top