This can actually be done in O(N):
sub tony_UCS { local $_ = reverse shift; my $longest = my $tohere = ""; while (my $char = chop $_) { my $idx = index($tohere, $char); if ($idx != -1) { $longest = $tohere if (length $tohere > length $longest); $tohere = substr($tohere, $idx + 1) . $char; } else { $tohere .= $char; } } $longest = $tohere if (length $tohere > length $longest); return $longest; }
This is about 3 times faster on a short test string, and quickly goes up to 5 times faster for slightly longer strings...
Tony
In reply to Re: Unique-Character Substring
by salvadors
in thread Unique-Character Substring
by japhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |