2023-11-09 Daily Challenge

Today I have done leetcode's November LeetCoding Challenge with cpp.

November LeetCoding Challenge 9

Description

Count Number of Homogenous Substrings

Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.

A string is homogenous if all the characters of the string are the same.

A substring is a contiguous sequence of characters within a string.

 

Example 1:

Input: s = "abbcccaa"
Output: 13
Explanation: The homogenous substrings are listed as below:
"a"   appears 3 times.
"aa"  appears 1 time.
"b"   appears 2 times.
"bb"  appears 1 time.
"c"   appears 3 times.
"cc"  appears 2 times.
"ccc" appears 1 time.
3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.

Example 2:

Input: s = "xy"
Output: 2
Explanation: The homogenous substrings are "x" and "y".

Example 3:

Input: s = "zzzzz"
Output: 15

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase letters.

Solution

class Solution {
  const int MOD = 1e9 + 7;
public:
  int countHomogenous(string s) {
    int count = 0;
    int answer = 0;
    int current = s.front();
    for(auto c : s) {
      if(current == c) {
        count += 1;
      } else {
        answer += 1LL * count * (count + 1) / 2 % MOD;
        answer %= MOD;
        count = 1;
        current = c;
      }
    }
    answer += 1LL * count * (count + 1) / 2 % MOD;
    answer %= MOD;
    return answer;
  }
};

// Accepted
// 84/84 cases passed (24 ms)
// Your runtime beats 71.96 % of cpp submissions
// Your memory usage beats 53.48 % of cpp submissions (12 MB)