[Java/C++/Python] DP, 4 lines O(N) Time, O(1) Space
Intuition\nAn easier or harder version of 940. Distinct Subsequences II\n\n\n# Explanation\nthe subsequence 0 is tricky,\nso firstly we only count the case with
# **Intuition**\nAn easier or harder version of [940. Distinct Subsequences II](https://leetcode.com/problems/distinct-subsequences-ii/discuss/192017)\n<br>\n\n# **Explanation**\nthe subsequence 0 is tricky,\nso firstly we only count the case without any leading 0\n\nWe count the number of subsequence that ends with 0 and ends with 1.\n\nIf we meed 0,\nwe can append 0 to all existing `ends0 + ends1` subsequences,\nand make up `ends0 + ends1` different subsequences that ends with 0.\nans these subsequences has length >= 2,\nSo `ends0 = ends0 + ends1`.\n\nIf we meed 1, the same,\nwe can append 1 to all existing `ends0 + ends1` subsequence,\nand make up `ends0 + ends1` different subsequence that ends with 1,\nans these subsequences has length >= 2.\nSo `1` is not in them, `ends1 = ends0 + ends1 + 1`.\n\nThe result is `ends1 + ends2`, and don\'t forget about `0`.\nThe final result is `ends1 + ends2 + has0`.\n<br>\n\n# **Complexity**\nTime `O(n)`\nSpace `O(1)`\n<br>\n\n**Java**\n```java\n    public int numberOfUniqueGoodSubsequences(String binary) {\n        int mod = (int)1e9 + 7, ends0 = 0, ends1 = 0, has0 = 0;\n        for (int i = 0; i < binary.length(); ++i) {\n            if (binary.charAt(i) == \'1\') {\n                ends1 = (ends0 + ends1 + 1) % mod;\n            } else {\n                ends0 = (ends0 + ends1) % mod;\n                has0 = 1;\n            }\n        }\n        return (ends0 + ends1 + has0) % mod;\n    }\n```\n**C++**\n```cpp\n    int numberOfUniqueGoodSubsequences(string binary) {\n        int mod = 1e9 + 7, dp[2] = {0, 0};\n        for (char& c: binary)\n            dp[c - \'0\'] = (dp[0] + dp[1] + c - \'0\') % mod;\n        return (dp[0] + dp[1] + (binary.find("0") != string::npos)) % mod;\n    }\n```\n**Python**\n```py\n    def numberOfUniqueGoodSubsequences(self, b):\n        mod = 10**9 + 7\n        dp = [0, 0]\n        for c in b:\n            dp[int(c)] = (sum(dp) + int(c)) % mod\n        return (sum(dp) + (\'0\' in b)) % mod\n```\n\nIf you have any questions, feel free to ask. \nIf you like solution and explanations, please **Upvote**!\n
[]