diff options
Diffstat (limited to 'num_substrings_only_1.c')
-rw-r--r-- | num_substrings_only_1.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/num_substrings_only_1.c b/num_substrings_only_1.c new file mode 100644 index 0000000..2bb0698 --- /dev/null +++ b/num_substrings_only_1.c @@ -0,0 +1,34 @@ +// 1513. Number of Substrings With Only 1s +#include "leetcode.h" + +/* + * 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. + */ + +int numSub(char *s) { + int n = strlen(s), mod = 1e9 + 7, ans = 0, one_cnt = 0; + for (int i = 0; i < n; i++) { + if (s[i] == '1') + one_cnt++; + else { + if (one_cnt) { + long long val = (long long)(one_cnt + 1) * one_cnt / 2; + ans = (ans + val % mod) % mod; + } + one_cnt = 0; + } + } + if (one_cnt) { + long long val = (long long)(one_cnt + 1) * one_cnt / 2; + ans = (ans + val % mod) % mod; + } + return ans; +} + +int main() { + char *s1 = "0110111", *s2 = "101", *s3 = "11111"; + printf("%d\n", numSub(s1)); // expect: 9 + printf("%d\n", numSub(s2)); // expect: 2 + printf("%d\n", numSub(s3)); // expect: 21 +} |