a
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport java.util.*;\n\n\npublic class Solution {\n\n    // separate [0] as sequence and only count those without starting 0\n    // after that the first 1 MUST be taken\n    // finally count UNIQUE sequences only!!!!!!\n    // use same approach as before: group by the ending digit so:\n    // 101\n    // 113           ending at      1   11     101\n    // 124           total\n    public int numberOfUniqueGoodSubsequences(String s) {\n        int n = s.length();\n        int i = 0;\n        while (i < n && s.charAt(i) == \'0\') i++;\n        boolean hasZero = i > 0; // skip the first 1\n        if (i ==n) return 1; // only 0s\n\n        i++; // skip the first one and pretend it is ""; then manually adjust the calculation for every next 1 to allow starting new seq\n        long numEnding0 = 0;\n        long numEnding1 = 1;\n        long total = 1;\n        for (; i < n; i++) {\n            if (s.charAt(i) == \'0\') {\n                hasZero = true;\n                long newlyAdded = (total - numEnding0 + M) % M;\n                numEnding0 = total; // +=(total - prevNumEnding0) = numEnding0 + total - numEnding0 = total\n                total = (total + newlyAdded) % M; // total + newlyAdded\n            } else {\n                long newlyAdded = (total - numEnding1 + M + 1) % M; // +1 \n                numEnding1 = (total + 1) % M; // +1 to manually adjust the calculation for every next 1 to allow starting new seq\n                total = (total + newlyAdded) % M; // total + newlyAdded\n            }\n        }\n\n        return (int) ((total + (hasZero ? 1 : 0)) % M);\n    }\n\n    int M = (int) 1e9 + 7;\n\n}\n\n\n```
['Java']