You are viewing a single comment's thread. Return to all comments →
I like this approach; you could either set the endpoint of the for loop to i<l-m or just compare s.length==m in the if to avoid going "off the edge."
for
i<l-m
s.length==m
if
I found a very one-line functional approach, ugly but appealing all the same. I didn't put in the off-the-edge check for brevity :)
return s.filter((e,i,a) => (a.slice(i,i+m).reduce((aa,cc)=>aa+cc)==d)).length
Seems like cookies are disabled on this browser, please enable them to open this website
Subarray Division
You are viewing a single comment's thread. Return to all comments →
I like this approach; you could either set the endpoint of the
for
loop toi<l-m
or just compares.length==m
in theif
to avoid going "off the edge."I found a very one-line functional approach, ugly but appealing all the same. I didn't put in the off-the-edge check for brevity :)
return s.filter((e,i,a) => (a.slice(i,i+m).reduce((aa,cc)=>aa+cc)==d)).length