1. Introduction

The Count Group Occurrences problem is a string manipulation problem that helps us understand how to identify consecutive characters and count their groups.

Given a string s and an integer k, we need to count the number of distinct characters that appear in exactly k groups.

A group consists of one or more consecutive occurrences of the same character.

For example:

Code
s = "aaabbcca"

The groups are:

  • "aaa" – First group of a
  • "bb" – First group of b
  • "cc" – First group of c
  • "a" – Second group of a

Therefore:

  • Character a appears in 2 groups.
  • Character b appears in 1 group.
  • Character c appears in 1 group.

If k = 2, the answer is 1 because only character a appears in exactly two groups.

2. Problem Understanding

We are given:

  • A string s containing lowercase English letters.
  • An integer k representing the required number of groups.

Our task is to return the number of distinct characters whose group count is exactly equal to k.

Important: We must count groups, not individual character occurrences.

For example:

Code
s = "aaaaa"
k = 2

Although a appears five times, all five occurrences are consecutive and form only one group.

Therefore, the output is:

Code
0

3. Approach – Using a Frequency Array

We can solve this problem efficiently using an integer array of size 26.

Since the input contains only lowercase English letters, we need only 26 positions to store the group count of each character.

Step 1: Create an array

C#
int[] count = new int[26];

Each array index represents a lowercase English character.

IndexCharacter
0a
1b
2c
......
25z

Step 2: Identify new groups

We traverse the string using a for loop.

A new group starts when:

  1. We are processing the first character.
  2. The current character is different from the previous character.

Condition:

C#
if (i == 0 || s[i] != s[i - 1])

Whenever this condition is true, we increment the corresponding character's group count.

C#
count[s[i] - 'a']++;

Why s[i] - 'a'?

In C#, characters have numeric Unicode values.

Subtracting 'a' converts a lowercase letter into an array index.

For example:

C#
'a' - 'a' = 0
'b' - 'a' = 1
'c' - 'a' = 2

This allows us to access the correct position in the array.

Step 3: Count characters with exactly k groups

After processing the complete string, traverse the array.

If a character's group count equals k, increment the result.

C#
if (count[i] == k)
{
    result++;
}

Finally, return the result.

4. Complete C# Solution

C#
class Solution
{
    public int getCount(string s, int k)
    {
        // Store the number of groups for each character
        int[] count = new int[26];

        // Traverse the string
        for (int i = 0; i < s.Length; i++)
        {
            // Check whether a new group starts
            if (i == 0 || s[i] != s[i - 1])
            {
                count[s[i] - 'a']++;
            }
        }

        // Count characters having exactly k groups
        int result = 0;

        for (int i = 0; i < 26; i++)
        {
            if (count[i] == k)
            {
                result++;
            }
        }

        return result;
    }
}

5. Dry Run Example

Input:

Code
s = "geeksforgeeks"
k = 2

The string is divided into consecutive groups:

Code
g | ee | k | s | f | o | r | g | ee | k | s

Now count the groups for each character.

CharacterNumber of groups
g2
e2
k2
s2
f1
o1
r1

Since k = 2, we select the characters whose group count equals 2.

Code
g, e, k, s

Therefore:

Code
Output: 4

6. Another Example

Input:

Code
s = "aaabbcca"
k = 2

Groups:

Code
aaa | bb | cc | a

Group counts:

CharacterNumber of groups
a2
b1
c1

Only a appears in exactly two groups.

Code
Output: 1

7. Edge Case

Input:

Code
s = "aaaaa"
k = 2

All characters are consecutive.

Code
aaaaa

There is only one group of a.

Since 1 is not equal to 2:

Code
Output: 0

8. Complexity Analysis

Time Complexity: O(n)

  • We traverse the input string once, taking O(n).
  • We traverse the fixed-size array of 26 elements, taking O(26).
  • Therefore, total time complexity is O(n).

Auxiliary Space Complexity: O(1)

  • We use an integer array of size 26.
  • The array size remains constant regardless of the input length.

Therefore, auxiliary space complexity is O(1).

9. Important Concepts Learned

By solving this problem, we understand:

  1. String Traversal: How to iterate through a string using a loop.
  2. Consecutive Character Checking: How to identify changes between adjacent characters.
  3. Frequency Array: How to use an array to count occurrences efficiently.
  4. Character Arithmetic: How to convert lowercase characters into array indexes using s[i] - 'a'.
  5. Time and Space Complexity: How to achieve O(n) time and O(1) auxiliary space.

10. Conclusion

The Count Group Occurrences problem can be solved efficiently using a simple frequency array and consecutive character comparison.

The most important logic is:

C#
if (i == 0 || s[i] != s[i - 1])
{
    count[s[i] - 'a']++;
}

This condition ensures that we count only the beginning of each new group rather than every individual character.

Final Complexity:

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

This is a useful beginner-level problem for strengthening string manipulation, array counting, and character comparison concepts in C#.