How to count # of patterns in a sequence

Dec 2019
1
0
USA
Definition: Sequence 'ab' has patterns 'a', 'b', 'ab' but not 'ba', or by codes
Python:
def has_pattern(pat, lst):
  pat_i = 0
  for el in lst:
    if el == pat[pat_i]:
      pat_i += 1
      if pat_i == len(pat):
        return True
      continue
  return False
What I want is given a pattern and a sequence, the probability of a permutation of the sequence has the pattern. This function does the job
Python:
def prob(pat, lst):
  all_perm = list(itertools.permutations(lst, len(lst)))
  return Fraction(sum((has_pattern(pat, p) for p in all_perm)), len(all_perm))
but it's slow and I want a general formula.
What I can see is
1. Letters in the sequence but not in the pattern doesn't matter. prob('ab', 'abc') = prob('ab', 'ab')
2. The order of pattern doesn't matter. prob('ab', 'ab') = prob('ba', 'ab')
3, then?
Thanks.