Basic Question: Is there any simple way in Perl to find the most common substring?

Complex Question: I'd really like to take a long number (say 100,000 digits long) and find the least and the most common strings of a given length (say 5). The three approaches that come to mind are:

  1. Set up a database with all possible numbers of length 5 and run regex's against the string, updating the database as I go (requires 10^5 passes of the string)
  2. Move across the string once and read all possible 5-digit strings, storing them in a DB as I go. Comparing the contents of the DB-table with a list of all possible 5-mers should identify any "missing" 5-digit strings.
  3. Search the string for the most common word, mask it somehow, and then repeat until the entire string is "masked"

Somehow the last approach seems most "elegant" to me, but I'm fuzzy on it. But general comments on how to go about this are totally helpful. I had troubles figuring out what terms to google to find out about algorithms that might do this sort of "parsing of all substrings".


In reply to Most common substring by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.