[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] longest substring with unique characters



At 09:24 PM 1/19/01 +0100, abigail@foad.org wrote:
>There's no need to disassemble the string yourself, one regex will do:
>
>#!/opt/perl/bin/perl -w
>
>use strict;
>
>my $max = 0; $_ = shift;
>while (/\G(.)(?=((?:(?!\1).)*))/g) {$max = $1.$2 if length $max < length 
>$1.$2}
>print $max, "\n";
>
>__END__

Dammit Abigail, give us slower folks a chance once in a while :-)

#!/usr/bin/perl -wl

$_=shift;
do {
     $s='';
     /((.+?)(??{push @{$l[length $2]},$2; $s.=$2; "[$s]"}))/;
} while s/.//;
print "@{$l[-1]}";

__END__

It gets somewhat longer under strict since (??{}) doesn't seem to see lexicals.

Well, at least it prints all the longest substrings...

--
Peter Scott
Pacific Systems Design Technologies


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe