in reply to Finding longest palindrome from a string
A slight modification of my previous entry yields the winning entry (so far). :-)
sub { my $str = shift; my $rts = reverse $str; my $palindrome = ''; my $minlen = 3; for my $rotate_count ( 0 .. length( $str ) - 1 ) { my $mask = $str ^ $rts; # to distinguish adjacent palindromes substr $mask, $rotate_count, 0, "\1"; while ( $mask =~ /\0{$minlen,}/g ) { my $offs = $-[0]; --$offs if $offs > $rotate_count; # compensate for mark +er $palindrome = substr $str, $offs, $+[0] - $-[0]; $minlen = 1 + length $palindrome; } substr $rts, 0, 0, chop $rts; } return $palindrome; }
Makeshifts last the longest.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Finding longest palindrome from a string
by Aristotle (Chancellor) on Aug 14, 2004 at 02:09 UTC | |
Re^2: Finding longest palindrome from a string
by ccn (Vicar) on Aug 14, 2004 at 08:44 UTC | |
by Aristotle (Chancellor) on Aug 14, 2004 at 08:59 UTC | |
by ccn (Vicar) on Aug 14, 2004 at 09:40 UTC | |
by Aristotle (Chancellor) on Aug 14, 2004 at 09:48 UTC |
In Section
Seekers of Perl Wisdom