

# This one was fun. Mine's slow as can be, but it seems to work:

sub CountSub {
        my ($org,$sub) = @_;
        my $res=$org;
        $res =~ s/\Q$sub\E//g;
        return (length($org)-length($res))/length($sub);
}

sub repeated_substring {
        my $target = shift;
        my $first="";
        my @ok=();
        my $iteration=1;
        my $last = length($target)-2;
        for my $i (0 .. $last) { push @ok,$i; }
        if ($last>-1) {
                if (substr($target,0,1) eq substr($target,1,1)) {
                        $first=substr($target,0,1);
                }
        }
        while (scalar(@ok) > 1) {
                @nok=();
                foreach my $i (@ok) {
                        if ($i + $iteration < $last+1) {
                          my $subber=substr($target,$i,$iteration);
                          if (CountSub($target,$subber) > 1) {
                                push @nok, $i;
                                $first=$subber if ($iteration>length($first));
                          }
                        }
                }
                return $first if @ok==@nok;
                $iteration++;
                @ok=();
                @ok=@nok;
        }

        return $first;

}
