What have you tried? What didn't work? We are not a code writing service.
If you need to generate permutations, you can do that pretty easily with glob. That combines with regular expressions pretty easily - have you read perlretut? Also, for your example, I can't get your count of 5 for AAA -- I see either 4 or 6 depending on how you count overlaps.
Update: Stupid failing eyes. I counted two AAAs in the third word.
Node text goes above. Div tags should contain sig only
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>