# 
# Just for fun, I tried out a regex method that I didn't even consider at
# first because I thought it would be too slow, but it turns out to be
# faster and uses less code:


sub repeated_substring {
    my ($str) = @_;
    my $longest;
    my $word_len = 2;

    # Calculate the length for later.
    my $str_len = length($str);

    # Test until our index requires a match beyond the end of the string
    for (my $idx=0; $idx<=($str_len - 2*$word_len); $idx++) {
        my $test = substr($str, $idx, $word_len);

        # Use \Q and \E in case there is funky stuff in $str.
        while ($str =~ /(\Q$test\E).*?\1/) {
            $longest = $test;
            $test = substr($str, $idx, ++$word_len);
        }
    }

    return $longest;
}

