# 1513. Number of Substrings With Only 1s

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mkw6hPvZoysxTbKjE4g%2F-Mkw9NT6L3YCgt9TqZ_P%2Fimage.png?alt=media\&token=89cd7a53-2dfe-4705-8860-7f700bed06c4)

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LekNH5IywF8mjBxFcnu%2F-Mkw6hPvZoysxTbKjE4g%2F-Mkw9QCORnX4xEJEM6R3%2Fimage.png?alt=media\&token=2f94463b-75f2-42da-af64-1206998a278d)

time: O(n)

space: O(1)

```java
class Solution {
    public int numSub(String s) {
        int result = 0;
        int n = s.length();
        
        int j = 1;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '0') {
                continue;
            }
            
            j = Math.max(j, i+1);
            while (j < n && s.charAt(j) == '1') {
                j++;
            }
            result = (result + (j - i)) % ((int)1e9 + 7);
        }
        return result;
    }
}
```
