aboutsummaryrefslogtreecommitdiff
path: root/count_num_homogenous_substrings.c
blob: 1c66ae26efaa7c3d1fd4ca344c6ffcea331d4a4e (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
// 1759. Count Number of Homogenous Substrings
#include "leetcode.h"

/*
 * given a string 's', return the number of homogenous substrings of 's'. since
 * thhe answer may be very large, return it modulo 10^9+7. a string is
 * homogenous if all the characters of the string are the same. a substring is a
 * continuous sequence of chharacters within a string.
 */

int countHomogenous(char *s) {
  int ans = 0, mod = 1e9 + 7, n = strlen(s), cnt = 1;
  for (int i = 1; i < n; i++) {
    if (s[i] == s[i - 1])
      cnt++;
    else {
      long val = (1 + cnt) * cnt / 2;
      ans = (ans + val % mod) % mod;
      cnt = 1;
    }
  }
  long long val = (long long)(1 + cnt) * cnt / 2;
  ans = (ans + val % mod) % mod;
  return ans;
}

int main() {
  char s1[] = {"abbcccaa"}, s2[] = {"xy"}, s3[] = {"zzzzz"};
  printf("%d\n", countHomogenous(s1)); // expect: 13
  printf("%d\n", countHomogenous(s2)); // expect: 2
  printf("%d\n", countHomogenous(s3)); // expect: 15
}