in reply to finding number of contiguous letters

"I need to take a string and find the number of 3 contiguous letters."

I notice that the results so far seem to rely upon an array for the result. I think that can lead to duplication- the array may have the same three letters at various indices, which will give an erroneously high result. Consider the test case "Mississippi", which has "Mis", "iss", "ssi", "sis", "sip", and "ppi"- 6 strings of three letters, eh? I think a hash is better to store the results, then count the number of keys.

  • Comment on Re: finding number of contiguous letters

Replies are listed 'Best First'.
Re^2: finding number of contiguous letters
by blazar (Canon) on May 23, 2007 at 20:33 UTC

    Well, just chain a map-based solution like "mine" with the usual grep-based "uniq" or absorb the latter into the map():

    my %saw; my @parts = map { my $s=substr $str, $_, 3; $saw{$s}++ ? () : $s } 0..length($str)-3;