views:

113

answers:

5

I was hoping someone with better math capabilities would assist me in figuring out the total possibilities for a string given it's length and character set.

i.e. [a-f0-9]{6}

What are the possibilities for this pattern of random characters?

+10  A: 
Hamish Grubijan
So 16^6?Tried that and to be honest the number seemed a little high.@Hamish GrubijanThat's the number I got, but it seemed high.
Andre
Well, 6 digits in decimal give you 000000 through 999999 (one million). 6 digits in Hex give you up to ffffff = ~ 16.7 million
Hamish Grubijan
@Andre In hexadecimal FFFFFF is 16777215, and since 0000000 is also a possibility then Hamish's answer is correct at 16777216.
AaronLS
@Andre: just for perspective, US local phone numbers match `[0-9]{7}` and - ignoring reserved sequences - there are 10^7 of those per area code. Combinatorics mount up quickly.
msw
+1  A: 

first: 000000 last: ffffff

This matches hexadecimal numbers.

mcandre
+2  A: 

The number of possibilities is the size of your alphabet, to the power of the size of your string (in the general case, of course)

assuming your string size is 4: _ _ _ _ and your alphabet = { 0 , 1 }: there are 2 possibilities to put 0 or 1 in the first place, second place and so on. so it all sums up to: alphabet_size^String_size

Protostome
I think you've got that backwards -- it's the size of the alphabet to the power of the size of the string.
JacobM
yep.. I did.. Edited
Protostome
+2  A: 

If you are just looking for the number of possibilities, the answer is (charset.length)^(length). If you need to actually generate a list of the possibilities, just loop through each character, recursively generating the remainder of the string.

e.g.

void generate(char[] charset, int length)
{
  generate("",charset,length);
}

void generate(String prefix, char[] charset, int length)
{
  for(int i=0;i<charset.length;i++)
  {
    if(length==1)
      System.out.println(prefix + charset[i]);
    else
      generate(prefix+i,charset,length-1);
  }
}
VeeArr
+1  A: 

For any given set of possible values, the number of permutations is the number of possibilities raised to the power of the number of items.

In this case, that would be 16 to the 6th power, or 16777216 possibilities.

JacobM