Counting all permutations of a substring

Chris Lasher chris.lasher at gmail.com
Wed Apr 5 17:07:22 EDT 2006


Hi all,
How can one count all the permutations of a substring in a string? For
a more concrete example, let's say
targetstr = 'AAA'
and
probestr = 'AA'

I want to consider how many times one can count probestr ('AA') in
targetstr ('AAA'). The value in this example is, obviously, 2: you can
match the first and second A's as 'AA' and also the second and third
A's as 'AA'. Compiling a regular expression of the probestr and doing a
findall(targetstr) will not work because the RE stops on the first
combination of 'AA' that it finds (the first and second A's of the
targetstr). The regular expression re.compile(r'A{2,}'} will not work,
either; it simply consumes all three A's as one match. The string
method count('AA') acts similarly to the findall of the 'AA' RE,
returning 1, as it only matches the first and second A's.

Any suggestions on how to get at that 2nd pair of A's and count it?
Humbly, I'm a bit stumped. Any help would be appreciated.

Many thanks in advance,
Chris




More information about the Python-list mailing list