smarter ?? faster - for that you'll have to benchmark...

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11138432 use warnings; sub randomstring { join '', map +(qw(a b))[rand 2], 1 .. shift } for (1 .. 10) { match( $_, randomstring(4), randomstring(4), 1); } sub match { my ($n, $x, $y, $m) = @_; $x =~ /(.+)$(??{ ($1 ^ substr $y, 0, length $1) =~ tr!\0!!c <= $m ? '' : '(*FAIL)' })/ and print "$n:($-[1],@{[ length $1 ]})($x,$y)\n"; }

Outputs:

1:(1,3)(abba,baaa) 2:(3,1)(bbbb,aaba) 3:(1,3)(abba,abab) 4:(1,3)(abbb,bbaa) 5:(2,2)(baba,bbbb) 6:(1,3)(bbab,aabb) 7:(0,4)(aaaa,aaab) 8:(2,2)(baba,baab) 9:(2,2)(aaba,baaa) 10:(1,3)(bbab,bbba)

I hope I understood your problem correctly...

Update: fixed $_ to $n


In reply to Re: Suffix-prefix matching done right by tybalt89
in thread Suffix-prefix matching done right by baxy77bax

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.