Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created November 16, 2025 22:44
Show Gist options
  • Select an option

  • Save Ifihan/571e64937020128b8eac329fc7271c51 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/571e64937020128b8eac329fc7271c51 to your computer and use it in GitHub Desktop.
Number of Substrings With Only 1s

Question

Approach

I scan the string once and accumulate lengths of consecutive '1' runs. For each run of length L the number of substrings containing only '1' inside that run is L*(L+1)//2. I sum those values while taking modulo 10**9 + 7 to avoid overflow, and return the result.

Implementation

Complexities

  • Time: O(n)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment