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

[FWP] longest substring with unique characters



Dan Chetlin brought up this challenge on #perl and I accepted.

This is my first round so it's a little long and no doubt slow:

my @lines = map { chomp; $_ } <DATA>;
LINE: foreach ( @lines ) {
  my @chars  = split //;
  my $length = $#chars;
  my $end    = $length;
  while ( --$end ) {
    my $start  = 0;
    while ( $start + $end <= $length ) {
      my $substr = join '', @chars[$start..$start+$end];
      $substr =~ /(.).*(\1)/ ? $start++ : do {
                                              print "$substr\n";
                                              next LINE;
                                             };
    }
  }
}
__DATA__
that
abcdefa
abcdabc
testing
this test

The output for this is correct, well it does only find the *first*
longest unique match:

tha
abcdef
abcd
esting
his te


-- 

print(join(' ', qw(Casey R. Tweten)));my $sig={mail=>'crt@kiski.net',site=>
'http://home.kiski.net/~crt'};print "\n",'.'x(length($sig->{site})+6),"\n";
print map{$_.': '.$sig->{$_}."\n"}sort{$sig->{$a}cmp$sig->{$b}}keys%{$sig};
my $VERSION = '0.01'; #'patched' by Jerrad Pierce <belg4mit at MIT dot EDU>


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