views:

245

answers:

6
R= repeats allowed -> 2
A= alphabet (1-10)
S= space = 4;

So we want example:

[1][1][4][5]
[1][7][4][5]
[5][1][4][5]

But need a fancy math formula to calculate this and all combinations ?

A: 

There are relatively few possible solutions (< 10000), so it should be ok to generate all words in A^4, then remove the words with more than 2 repeats.

OR

  • Generate the (ordered) combinations of N distinct words
  • Generate the permutations of that subset to get all the possibilities without duplicates

  • Do the same with N-1 words

  • For each element in these words, add a duplicate at all the positions but the position of the said character.
Guillaume
ja also thought about going a brute force method, but was want a more 'clever' solution, as what about cases where S(Space) = 200 then it becomes imensebut should still be WAYYYYY less than if we allow all repeats
piet
"Generate the (ordered) combinations of N distinct words" ? ordered ? Sorry can you explain more or maybe give example ?Me being dumb :(
piet
Ordered means words are sorted (w1 < w2 < w3 ...), so 123 is a possibility but 231 isn't.
Guillaume
+3  A: 

As I understand it, your alphabet is 1 .. 10, with each 'letter' possibly occurring twice. So what you really have is an alphabet that is ...

1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10

It has a length of 20, not 10.

The problem now becomes 20 permute 4.

Hope this helps.

EDIT: As per your additional comments to your question, you can then check each generated permutation to see if it is of the form XXYY as that would be invalid according to what you have written.

Sparky
ahhh it's starting to make sense, thanx :)
piet
This answer is on the right track, but unfortunately incorrect, since 20 permute 4 assumes all elements are distinct, which they are not.
BlueRaja - Danny Pflughoeft
my head is going to explode :(Anyway to calculate the amount of permutation for the above/original values ?
piet
BlueRaja--you are correct. Although tempted to delete my answer as it has been properly pointed out how it is incorrect, I will leave it here in hopes that others will learn from my mistakes.
Sparky
+2  A: 

The total number is
N * [ (S choose 1) * ((N-1) permute (S-1)) + (S choose 2) * ((N-1) permute (S-2)) + ... + (S choose R) * ((N-1) permute (S-R)) ]

  • In otherwords, probably best to fix 1 of the repeated item in place (S choose 1 different ways of doing this) and permute the remaining N-1 items over the S-1 remaining spaces; (same as normal N permute S)

  • then fix 2 of your identical item in place (S choose 2 different ways of doing this) and permute the remaining N-1 items over the remaining S-2 spaces.

  • etc for each possible number of repeated items, from 1 up to R

  • And then there are N choices for your possible repeated item.

You can use this algorithm to enumerate the possibilities too.

Edit

Oh dear. Thanks @blueraja, you are absolutely correct! the n-repeated-items case does not generalise to 1 item!

corrected formula is therefore

(N permute S)
+ N * [ (S choose 2) * ((N-1) permute (S-2)) 
      + (S choose 3) * ((N-1) permute (S-3)) 
      + ... 
      + (S choose R) * ((N-1) permute (S-R)) ]
Sanjay Manohar
sorry for multiple edits - got my permutes and chooses muddled!
Sanjay Manohar
Wow Nice count-algo ! !
piet
This is much closer to a correct answer. However, notice that the string `1 2 3 4` gets counted four times: once if we consider `1` the fixed element, once if we consider `2` the fixed element, etc...
BlueRaja - Danny Pflughoeft
+1 for correct answer, but -1 for giving out blunt answer to (what I assume is a) homework problem :)
BlueRaja - Danny Pflughoeft
+4  A: 

A correct general answer requires a summation. I will show you how to do it for these particular values, and let you generalize it.

There are two cases:

  • Permutations containing no duplicates. This is just 10 P 4
  • Permutations containing exactly one duplicate:
    • Choose which number is the duplicate: 10 C 1
    • Choose two places for it: 4 C 2
    • Choose the numbers which fit into the remaining two places: 9 P 2

Thus the answer to this particular case is 9360.

BlueRaja - Danny Pflughoeft
Excellent :) Can you also post the math to arrive at 9360 ?
piet
Comeon Jacques, you can do that part. Remember, you multiply ANDs, and add ORs *(the two cases are ORed together, but within each case each condition is an AND)*
BlueRaja - Danny Pflughoeft
A: 

I assume that only one item can repeat, and this item is not predetermined.

Here is a formula that works on A(alphabet size), S(strings size) and R(maximum repetition count):

f(A,S,R) = (A perm S) + A*Sum[r=2 to R] ( (S choose r)*(A-1 perm S-r) )

For example, for R=1 (simple permutation) we get f(A,S,R)=(A perm S) as expected. For A=S=R=2 we have f(A,S,R)=4 which corresponds to:

1,2

2,1

1,1

2,2

The case you describe in the question is A=10, R=2, S=4, and then we have:

f(A,S,R) = 9360

(Exactly as BlueRaja calculated)

Eyal Schneider
A: 

This is a nice article about combinations and permutations, there you can find all the formulas

Enrique