use v6; my @strA = $*IN.get.comb; my @strB = $*IN.get.comb; my %offsetB; %offsetB.push( @strB Z 0..* ); my $max-substr = ''; for 0..* Z @strA -> $i, $chr { next unless %offsetB.exists: $chr; for @( %offsetB{$chr} ) -> $j { my $substr = [~] gather for @strA[$i..*-1] Z @strB[$j..*-1] -> $a, $b { last if $a ne $b; take $a; }; $max-substr = $substr if $substr.chars > $max-substr.chars; } last if $max-substr.chars >= +@strA | +@strB - $i; } say $max-substr;