in reply to how to count the number of repeats in a string (really!)
The regex engine is quite adept at backtracking to find all inputs that satisfy a set of conditions. The shortest solution should be a regex one.
my $str = 'aabcdabcabcecdecd'; local our %counts; $str =~ / (.{2,}) # or (.+) (?(?{ !$counts{$1} })(?=.*\1)) (?{ ++$counts{$1} }) (?!) /x; use Data::Dumper; print Dumper \%counts;
Update:
(?(?{ !$counts{$1} })(?=.*\1))
might be more efficient as
(?> (?(?{ !$counts{$1} })(?=.*\1)) )
Update: The above doesn't work. $str='ababa' fails. My simpler version doesn't suffer from this bug.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to count the number of repeats in a string (really!)
by ikegami (Patriarch) on Nov 14, 2007 at 19:28 UTC | |
by blazar (Canon) on Nov 14, 2007 at 21:49 UTC |