aboutsummaryrefslogtreecommitdiff
path: root/num_substrings_only_1.c
blob: 2bb0698e71f59be8079fd0966b25f668beaa363f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
}