I decided to simplify the code using regex, then reread itertools documentation,

itertools — Functions creating iterators for efficient looping — Python 3.8.1 documentation , to find the following

"Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation."

That means all_perm in your code was a list with some duplicate elements.

ie.

Python:

```
print(list(permutations('aa')))
> [('a', 'a'), ('a', 'a')]
```

This slowed down your code and caused a defect, namely prob('ab','aabbcc') isn't 47/90

My code yields 23/90. It defines all_perm as a set (removes duplicates) from a list of joined strings. It then calls pattern_count_re which uses regex search.

Python:

```
from itertools import permutations
from fractions import Fraction
from re import search
def pattern_count_re(pat,all_perm):
count = 0
for perm in all_perm:
# print(perm)
if search(pat,perm):
count +=1
return count
def prob_re(pat, seq):
all_perm = set(list(map("".join, permutations(seq))))
return Fraction(pattern_count_re(pat,all_perm), len(all_perm))
print(prob_re('abc','aabbcc'))
```

The math way:

How many permutations?

\(\displaystyle \frac{n!}{x_1!x_2!...}\)

Where n is the length of the sequence and \(\displaystyle x_i\) is number of repeats per letter

So for 'aabbcc' we have

\(\displaystyle \frac{6!}{2!2!2!} = 90\)

For counting 'abc' in all permutations of 'aabbcc' we have 4 ways to put 'abc' in the string of length 6 (excluding wrap-around).

We then have 6 ways to permute remaining letters a,b,c from original sequence.

6*4 = 24, but 'abcabc' is repeated twice, so it's 24-1 = 23

I am stumped on how to count patterns in all permutes generally, but I hope the above example is at least a start for you (if you want the math closed form solution)