[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
Re: [FWP] longest substring with unique characters
On Fri, Jan 19, 2001 at 03:34:24PM +0100, Julien Quint wrote:
> #!/usr/bin/perl -w
>
> use strict;
>
> while (<>) {
> chomp;
> my $longest = "";
> while (/./g) {
> my $match = $&;
> my $substr = $';
> ($match, $substr) = ($match . $&, $') while $substr =~ /^[^$match]/;
> $longest = $match if length($match) > length($longest);
> }
> print "$longest\n";
> }
>
> __END__
>
> I guess this version is more perlish.
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__
==== Want to unsubscribe from Fun With Perl? Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
==== unsubscribe