Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Detecting transpositions

by Cine (Friar)
on Aug 06, 2003 at 15:45 UTC ( [id://281453]=note: print w/replies, xml ) Need Help??


in reply to Re: Detecting transpositions
in thread Detecting transpositions

Or even shorter... Albeit about 3 times slower on long strings and 30% faster for short...
sub comp { local $_ = $_[0] ^ $_[1]; return s/([^\x00])\1//g==1 && !tr/\x00/1/c; }


T I M T O W T D I

Replies are listed 'Best First'.
Re: Detecting transpositions
by Abigail-II (Bishop) on Aug 06, 2003 at 15:52 UTC
    That doesn't return the index of where the transposition happens, does it?

    Abigail

      So here it is with bonus, just a little longer... And now, its just a little faster than yours. Except on large identical strings where it is 3 times faster because of the first line. Without it the regexp would perform very slow.
      sub comp { return -1 if $_[0] eq $_[1]; local $_ = $_[0] ^ $_[1]; /^([\x00]*)([^\x00])\2[\x00]*$/; $1 ? length$1 : -1; }


      T I M T O W T D I
      No, that was only a bonus ;)

      T I M T O W T D I

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://281453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found