Lintcode - 1870 · Number of Substrings with All Zeroes

https://www.lintcode.com/problem/1870/

time: O(n)

space: O(1)

public class Solution {
    /**
     * @param str: the string
     * @return: the number of substrings 

     00010011
     */
    public int stringCount(String str) {
        int result = 0;
        int n = str.length();

        int j = 1;
        for (int i = 0; i < n; i++) {
            if (str.charAt(i) != '0') {
                continue;
            }
            j = Math.max(j, i+1);
            while (j < n && str.charAt(j) == '0') {
                j++;
            }
            result += j - i;
        }
        return result;
    }
}

Last updated