Problems? Is your data what you think it is? PerlMonks

### Comment on

 Need Help??

Here's my best attempt so far:

sub nCommonSubstrLenL { my( \$haystack, \$needle, \$len ) = @_; ( \$haystack, \$needle ) = ( \$needle, \$haystack ) if length( \$haystack ) < length( \$needle ); # Added my \$count = 0; my %possibles; for my \$ni ( 0 .. length( \$needle ) - \$len ) { my \$possible = substr( \$needle, \$ni, \$len ); next if ++\$possibles{ \$possible } > 1; ++\$count if 1+index \$haystack, \$possible; } return \$count; }

Update: A slightly faster reformulation. Updated again to work for lengths other than 2.

sub nCommonSubstrLenL2 { my( \$haystack, \$needle, \$len ) = @_; ( \$haystack, \$needle ) = ( \$needle, \$haystack ) if length( \$haystack ) < length( \$needle ); ## Added. # my \$pattern = "A\$len X" x int( length( \$needle ) / \$len ); my \$pattern = (" A\$len" . 'X' x ( \$len-1 )) x (length( \$needle ) - + \$len +1); my \$count = 0; my %possibles; for my \$possible ( unpack \$pattern, \$needle ) { next if ++\$possibles{ \$possible } > 1; ++\$count if 1+index \$haystack, \$possible; } return \$count; }

And buking for the bonus, a not well tested version that looks for all common substring equal or greater in length than the user specifed parameter:

sub nCommonSubstrGreaterLenL { my( \$haystack, \$needle, \$len ) = @_; my \$count = 0; my %possibles; for my \$l ( \$len .. length( \$needle ) ) { my \$sofar = \$count; for my \$ni ( 0 .. length( \$needle ) - \$l ) { my \$possible = substr( \$needle, \$ni, \$l ); next if ++\$possibles{ \$possible } > 1; ## print( "\$possible : \$count" ), ++\$count if 1+index \$haystack, \$possible; } last unless \$count > \$sofar; } return \$count; } print 'Buk:', nCommonSubstrGreaterLenL 'ABCDEF','ABDEFCBDEAB', 2; __END__ AB : 0 DE : 1 EF : 2 DEF : 3 Buk:4

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Challenge: Fast Common Substrings by BrowserUk
in thread Challenge: Fast Common Substrings by Limbic~Region

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2018-02-20 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (267 votes). Check out past polls.

Notices?