I am a python programmer,trying to upgrade a python program I created.

I am aware the total number of permutations can be calculated using 

```
n^r 

```

where n is the total number of things to choose from and r is how many we choose..

**for example consider the string "abc" where need to generate three letter "words"..the result below**

```
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc

```

**Question**

What I am trying to accomplish is to derive a formula for the total number of permutations where each permutation has maximum nth characters of the same identity..consider this example

for example the total number of permutations that has only **two letters** of the same type repeating....

This is my results for generating all permutations from my program with two letters of the same type reaming

```
aab - only two 'a'
aac - only two 'a'
aba - onyl two 'a'
abb - only two 'b'
abc - maximum is two letters repeating..also good
aca - maximum is two letters repeating..also good
acb - maximum is two letters repeating..also good
acc - maximum is two letters repeating..also good
baa- .maximum is two letters repeating..also good
bab ..................
bac....................
bba
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb

```

**TOTAL NUMBER OF PERMUTATION IS 24**
I can't seem to derive a way to do this BEFORE outputing the results..

**In a nutshell**

I want to find A FORMULA for the number of permutation that only has nth letters in each permutation repeating not more than nth of the same type..

I want to precalculate the **total number of permutations** where each permutation has nth letter of the same type remaining and output this to the user of my program

---

My program is bulky but here is the permutation codes I am using (note I am using itertools from python built in libraries)

```
for perm in itertools.product(string,repeat=self.word_length):
    if args:
        args[0]()                    
    #print("after",self.counter)
    self.counter=self.counter+1
    result1=[perm.count(z) for z in perm]
    result1.sort(reverse=True)
    if result1[0]<=self.actual_repeat:
        yield perm

```

I am using itertools to generate the permutations , count each letter and find their fequency and filter for two letter words...the variable `self.actual repeat` is 2