Note that there is no requirement that the substrings be unique, only that each has no repeated characters internally.
Not going to bother handling arrays since an array is not a string, and the task description specifically says 'Given a string'.
sub abbreviate ($_) { .chars > 80ย ?? "(abbreviated)\n" ~ .substr(0,35) ~ ' ... ' ~ .substr(*-35)ย !! $_ }
sub longest ($string) {
return 0 => [] unless $string.chars;
my ($start, $end, @substr) = 0, 0;
while ++$end < $string.chars {
my $sub = $string.substr($start, $end - $start);
if $sub.contains: my $next = $string.substr: $end, 1 {
@substr[$end - $start].push($sub) if $end - $start >= @substr.end;
$start += 1 + $sub.index($next);
}
}
@substr[$end - $start].push: $string.substr($start);
@substr.pairs.tail
}
# Testing
say "\nOriginal string: {abbreviate $_}\nLongest substring(s) chars: ", .&longest
# Standard tests
for flat qww< xyzyabcybdfd xyzyab zzzzz a '' >,
# multibyte Unicode
< ๐๐ฉ๐๐ฉโ๐ฉโ๐ฆโ๐ฆ๐งข๐๐จโ๐งโ๐ง๐๐ฉโ๐ฉโ๐ฆโ๐ฆ๐ฉ๐๐๐งข ฮฑโฯถฮฑฯถ >,
# check a file
slurp 'unixdict.txt';
Original string: xyzyabcybdfd
Longest substring(s) chars: 5 => [zyabc cybdf]
Original string: xyzyab
Longest substring(s) chars: 4 => [zyab]
Original string: zzzzz
Longest substring(s) chars: 1 => [z z z z z]
Original string: a
Longest substring(s) chars: 1 => [a]
Original string:
Longest substring(s) chars: 0 => []
Original string: ๐๐ฉ๐๐ฉโ๐ฉโ๐ฆโ๐ฆ๐งข๐๐จโ๐งโ๐ง๐๐ฉโ๐ฉโ๐ฆโ๐ฆ๐ฉ๐๐๐งข
Longest substring(s) chars: 6 => [๐งข๐๐จโ๐งโ๐ง๐๐ฉโ๐ฉโ๐ฆโ๐ฆ๐ฉ]
Original string: ฮฑโฯถฮฑฯถ
Longest substring(s) chars: 3 => [ฮฑโฯถ โฯถฮฑ]
Original string: (abbreviated)
10th
1st
2nd
3rd
4th
5th
6th
7th
8t ... rian
zounds
zucchini
zurich
zygote
Longest substring(s) chars: 15 => [mbowel
disgrunt]