diff options
Diffstat (limited to 'num_substrings_only_1.py')
-rw-r--r-- | num_substrings_only_1.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/num_substrings_only_1.py b/num_substrings_only_1.py new file mode 100644 index 0000000..a405c9c --- /dev/null +++ b/num_substrings_only_1.py @@ -0,0 +1,22 @@ +# 1513. Number of Substrings With Only 1s + +""" +given a binary string 's', return the number of substrings with all +characters 1's since the answer may be too large, return it modulo 1e9+7. +""" + + +class Solution(object): + def numSub(self, s): + """ + :type s: str + :rtype: int + """ + return sum(len(a) * (len(a) + 1) / 2 for a in s.split("0")) % (10**9 + 7) + + +if __name__ == "__main__": + obj = Solution() + print(obj.numSub("0110111")) + print(obj.numSub("101")) + print(obj.numSub("111111")) |