2
def numberOfUniqueGoodSubsequences(self, binary: str) -> int:4
# zero_as_last: the count of S0, good sequence ending in 05
# one_as_last : the count of S1: good sequence ending in 16
# zero_exist: existence flag of 0 in given binary8
dp = {"zero_as_last": 0, "one_as_last": 0, "zero_exist": 0}10
for bit in map(int, binary):13
# current good = ( S0 concate with 1 ) + ( S1 concate with 1 ) + 1 alone14
# + "1" is allowed because leading 1 is valid by description15
dp["one_as_last"] = dp["zero_as_last"] + dp["one_as_last"] + 118
# current good = ( S0 concate with 0 ) + ( S1 concate with 0 )19
# + "0" is NOT allowed because leading 0 is invalid by description20
dp["zero_as_last"] = dp["zero_as_last"] + dp["one_as_last"]22
# check the existence of 023
dp["zero_exist"] |= 1 - bit25
return sum(dp.values()) % (10**9 + 7)