Mid/SeniorMathString

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 10^9 + 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
Inputs = "abbcccaa"
Output13
The homogenous substrings are counted as 3 occurrences of "a", 1 of "aa", 2 of "b", 1 of "bb", 3 of "c", 2 of "cc", and 1 of "ccc", totaling 13.
Example 2
Inputs = "xy"
Output2
The homogenous substrings are "x" and "y".

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase letters.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate