PHP, 133 bytes
#PHP, 133 bytes aa PHP port of @edc65´s JavaScript port of DLosc´s Pip answer.
function f($s){for(;$c=lcowbe[$i];)$a[$c]=substr_count($s,$c)>>!$i++;return min($a);}for($s=$argv[1];f($s)==f($s.=$s[$i++]););echo$s;
takes lower case input from command line argument. Run with -nr
.
breakdown
// function to count the cowbells:
function f($s)
{
for(;$c=lcowbe[$i];) # loop through "cowbel" characters
$a[$c]=substr_count($s,$c) # count occurences in $s
>>!$i++; # divide by 2 if character is "l" (first position)
return min($a); # return minimum value
}
for($s=$argv[1]; # copy input to $s, loop:
f($s) # 1. count cowbells in $s
== # 3. keep looping while cowbell counts are equal
f($s.=$s[$i++]) # 2. append $i-th character of $s to $s, count cowbells
;);
echo$s; # print $s