You are viewing a single comment's thread. Return to all comments →
The shortest method for the above python code
return sum(1 for x in range(len(s)) if sum(s[x:x+m]) == d)
Im not sure of this solution. This will not be working with this input:
1 2 1 3 3
The output should be 2 but will be 3 because it will count last digit of the series 3. This solution does not consider what if solution will be shorter than a month digit.
Sure, this will pass all validators but still... :).
return sum(1 for x in range(len(s)) if sum(s[x:x+m]) == d and len(s[x:x+m]) == m )
That't beutiful thank you for sharing : i did this way but we can make way shorter by yours..
List comprehension is amazing
I agree but also his method is n * m time complexity sadly.
a minor optimization:
return sum(1 for x in range(len(s)-m+1) if sum(s[x:x+m]) == d)