Skip to main content

Count Number of Homogenous Substrings - Solution & Explanation

MediumMathString17 min readAsked at: Google, Virtu
Practice this problem

Problem Statement

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.

Approach Overview

Problem Overview: Given a string s, count how many substrings consist of only one repeating character. A substring like "aaa" contributes multiple homogenous substrings: a, a, a, aa, aa, and aaa. The result must be returned modulo 1e9 + 7.

Approach 1: Two-Pointer Approach (O(n) time, O(1) space)

Traverse the string while tracking the length of the current run of identical characters. Use two pointers or a single index with a counter. When the current character matches the previous one, extend the run; otherwise reset the run length to 1. Each extension adds the current run length to the total count because every new character forms additional homogenous substrings ending at that index. This works because a run of length k contributes exactly k substrings ending at the current position.

This approach relies on simple iteration and constant memory, making it ideal for large inputs. The algorithm scans the string once, updates a running count, and applies the modulo constraint after each addition. It’s a common pattern when solving run-length problems on string data.

Approach 2: Mathematical Counting Approach (O(n) time, O(1) space)

Instead of counting substrings incrementally, group consecutive identical characters and compute their contribution using a formula. If a run of the same character has length k, the number of homogenous substrings inside that run equals k * (k + 1) / 2. Iterate through the string, measure each run length, apply the formula, and add the result to the answer.

This method separates the counting logic from traversal and emphasizes the combinatorial insight behind the problem. It is effectively run-length encoding combined with a simple math formula. The runtime remains linear because each character is processed once, and only a few integer variables are maintained.

Recommended for interviews: The two-pointer method is usually the expected solution because it demonstrates strong control over iteration and incremental counting in two-pointer style scanning. The mathematical approach shows deeper understanding of how substring counts arise from run lengths, which can make the reasoning clearer. Both run in O(n) time with O(1) space and are considered optimal.

Approach 1: Two-Pointer Approach

In this approach, we make use of two pointers to keep track of the start and end of a sequence of identical characters. As we iterate through the string, we update the end pointer when we find the same character, and on encountering a different character, we calculate the number of homogenous substrings formed using the formula for the sum of first n natural numbers: n * (n + 1) / 2, where n is the length of the sequence of identical characters. We repeat this process for each identified sequence and accumulate the total number of homogenous substrings.

In the C solution, we iterate through the string while maintaining the length of consecutive identical characters with the length variable. When a different character is encountered, we add the number of homogenous substrings formed by the previous sequence to count. The final count is taken modulo 10^9 + 7 as required.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as we only use a constant amount of extra space.

Try this approach in the editor β†’

Approach 2: Mathematical Counting Approach

This approach involves counting the sequences of homogenous substrings by leveraging arithmetic progression's sum. By identifying the start and end of each homogenous substring part, we can determine the total count for those characters. We iterate through the string, find the size of each segment, and calculate total substrings using the arithmetic formula.

The C code initializes a result variable and iterates through the string to compute lengths of continuous characters. The formula for an arithmetic progression then calculates homogenous substrings from each segment.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the length of the string.
Space Complexity: O(1) since no extra space is used apart from fixed variables.

Try this approach in the editor β†’

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C#

C

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Two-Pointer Approach

Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), as we only use a constant amount of extra space.

Mathematical Counting Approach

Time Complexity: O(n) where n is the length of the string.
Space Complexity: O(1) since no extra space is used apart from fixed variables.

Default Approachβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two-Pointer Run-Length CountingO(n)O(1)Best general solution. Single pass with incremental counting, common interview pattern.
Mathematical Run-Length FormulaO(n)O(1)When you prefer grouping characters first and computing substrings using the k*(k+1)/2 formula.

Video Solution

Count Number of Homogenous Substrings | Intuition | Math | Leetcode - 1759 β€’ codestorywithMIK β€’ 8,233 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Count Number of Homogenous Substrings easy or hard?
The problem is classified as Medium on LeetCode. The challenge lies in recognizing that consecutive character runs generate multiple substrings and deriving an efficient counting method instead of enumerating all substrings.
Count Number of Homogenous Substrings Python/Java solution
Both Python and Java implementations follow the same idea: scan the string, track the current run length, and add it to the result. Apply modulo 1e9+7 after each update. The algorithm remains O(n) time and O(1) space regardless of language.
How to solve Count Number of Homogenous Substrings in O(n)?
Iterate through the string while keeping a counter of consecutive identical characters. If the current character matches the previous one, increase the counter; otherwise reset it to 1. Add the counter value to the total for every position. Apply modulo 1e9+7 to prevent overflow.
What is the best approach for Count Number of Homogenous Substrings?
The optimal approach uses a run-length or two-pointer scan of the string. Track the length of consecutive identical characters and add the run length to the result at every step. This counts all homogenous substrings ending at the current index. The solution runs in O(n) time with O(1) space.
Is Count Number of Homogenous Substrings asked at Google/Amazon/Meta?
This problem represents a common string run-length counting pattern frequently used in interviews at large tech companies such as Amazon, Google, and Meta. Variants appear in substring counting or consecutive-character problems where candidates must derive counts from runs.
What data structure is used in Count Number of Homogenous Substrings?
No complex data structure is required. The solution mainly uses simple variables to track the length of consecutive characters while iterating through a string. The key concept is run-length counting combined with arithmetic accumulation.
What is the time complexity of Count Number of Homogenous Substrings?
The optimal algorithms run in O(n) time where n is the length of the string. Each character is processed exactly once while maintaining a counter for consecutive characters. Space complexity remains O(1) because only a few integer variables are used.

Ready to solve this problem?

Practice Count Number of Homogenous Substrings with our built-in code editor and test cases.

Practice on FleetCode