#JavaScript (ES6), 113107 126113 126 141
Edit 3 changed c function again. I thought the verbose length
and split
would be too lengthy. I was wrong.
Assuming lowercase input
a=>[...a].some(z=>c(a+=z)>b,c=a=>[c=a=>Math.min(..a].[...'lcowbe'].map(x=>k[x]=-~k[x](c,k={}i)&&Math=>~-a.minsplit(k.b,k.c,k.e,k.l>>1,k.o,k).wlength/-~!i)),b=c(a))&&a
a=>{
c=a=>({ // cowbell functions - count cowbells
k={},
k = [...a] 'lcowbe'].map(x(c,i) => k[x]
= (a.split(c).length -~k[x] 1), // store rep count occurrences of eachc charin a
// ... even if I only need those six/ (and!i l+ must1) be// divideddivide by 2 if first in list ('l')
);
return Math.min(k.b,k.c,k.e,k.l>>1,k.o,k.w);
)};
b = c(a); // starting number of cowbells
[...a].some(z => ( // iterate for all chars of a until true
a += z,
c(a) > b // exit when I have more cowbells
));
return a;
}
f=
a=>[...a].some(z=>c(a+=z)>b,c=a=>[c=a=>Math.min(...a][...'lcowbe'].map(x=>k[x]=-~k[x](c,k={}i)&&Math=>~-a.minsplit(k.b,k.c,k.e,k.l>>1,k.o,k).wlength/-~!i)),b=c(a))&&a
;["christopher walken begs for more cowbell!"
,"the quick brown fox jumps over the lazy dog"
,"cowbell"
,"cowbell cowbell cowbell"
,"cowbell cowbell cowbel"
,"bcelow"
,"abcdefghijklmnopqrstuvwxyz"
,"cccowwwwbbeeeeelllll"
,"be well, programming puzzles & code golf"
,"lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. wow!"
,`c-c-b-c
i have a cow, i have a bell.
uh! bell-cow!
i have a cow, i have a cowbell.
uh! cowbell-cow!
bell-cow, cowbell-cow.
uh! cow-cowbell-bell-cow.
cow-cowbell-bell-cow!
`].forEach(x=>console.log(x+'\n\n'+f(x)))