Longest substrings without repeating characters: Difference between revisions

m
→‎{{header|Raku}}: more memory efficient for long strings.
m (→‎{{header|Raku}}: more memory efficient for long strings.)
Line 90:
 
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'.
<lang perl6>sub abbreviate ($_) { .chars > 80 ?? "(abbreviated)\n" ~ .substr(0,35) ~ ' ... ' ~ .substr(*-35) !! $_ }
 
Line 98 ⟶ 100:
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);
}
Line 106 ⟶ 108:
}
 
# Testing
# Standard tests
say "\nOriginal string: {abbreviate $_}\nLongest substring(s) chars: ", .&longest
 
# Standard tests
for flat qww< xyzyabcybdfd xyzyab zzzzz a '' >,
 
# multibyte Unicode
'< 👒🎩🎓👩‍👩‍👦‍👦🧢🎓👨‍👧‍👧👒👩‍👩‍👦‍👦🎩🎓👒🧢' α⊆϶α϶ >,
 
# check a file
Line 134 ⟶ 137:
Original string: 👒🎩🎓👩‍👩‍👦‍👦🧢🎓👨‍👧‍👧👒👩‍👩‍👦‍👦🎩🎓👒🧢
Longest substring(s) chars: 6 => [🧢🎓👨‍👧‍👧👒👩‍👩‍👦‍👦🎩]
 
Original string: α⊆϶α϶
Longest substring(s) chars: 3 => [α⊆϶ ⊆϶α]
 
Original string: (abbreviated)
10,327

edits