Longest substrings without repeating characters: Difference between revisions
Longest substrings without repeating characters (view source)
Revision as of 12:55, 29 May 2021
, 3 years ago→{{header|Raku}}: more memory efficient for long strings.
Thundergnat (talk | contribs) 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
$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)
|