sub repeated_substring {
  my $str = shift;
  my %data;
  my ($max_start, $max_len) = (undef, 0);
  for my $s1 (0..length($str)-1) {
    my $ch = substr($str, $s1, 1);
    for my $s2 (@{$data{$ch}}) {
      my $len = $max_len ? $max_len+1 : 2;
      $len++
        while substr($str, $s1, $len) eq substr($str, $s2, $len)
        and   $s2+$len-1 < $s1;
      ($max_len, $max_start) = ($len, $s1) if --$len > $max_len;
    }
    push @{$data{$ch}}, $s1;
  }
  return $max_len ? substr($str, $max_start, $max_len) : undef;
}
