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:
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:
s = "aaaaa"
k = 2
Although a appears five times, all five occurrences are consecutive and form only one group.
Therefore, the output is:
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
int[] count = new int[26];
Each array index represents a lowercase English character.
| Index | Character |
|---|
| 0 | a |
| 1 | b |
| 2 | c |
| ... | ... |
| 25 | z |
Step 2: Identify new groups
We traverse the string using a for loop.
A new group starts when:
- We are processing the first character.
- The current character is different from the previous character.
Condition:
if (i == 0 || s[i] != s[i - 1])
Whenever this condition is true, we increment the corresponding character's group count.
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:
'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.
if (count[i] == k)
{
result++;
}
Finally, return the result.
4. Complete C# Solution
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:
s = "geeksforgeeks"
k = 2
The string is divided into consecutive groups:
g | ee | k | s | f | o | r | g | ee | k | s
Now count the groups for each character.
| Character | Number of groups |
|---|
| g | 2 |
| e | 2 |
| k | 2 |
| s | 2 |
| f | 1 |
| o | 1 |
| r | 1 |
Since k = 2, we select the characters whose group count equals 2.
g, e, k, s
Therefore:
Output: 4
6. Another Example
Input:
s = "aaabbcca"
k = 2
Groups:
aaa | bb | cc | a
Group counts:
| Character | Number of groups |
|---|
| a | 2 |
| b | 1 |
| c | 1 |
Only a appears in exactly two groups.
Output: 1
7. Edge Case
Input:
s = "aaaaa"
k = 2
All characters are consecutive.
aaaaa
There is only one group of a.
Since 1 is not equal to 2:
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:
- String Traversal: How to iterate through a string using a loop.
- Consecutive Character Checking: How to identify changes between adjacent characters.
- Frequency Array: How to use an array to count occurrences efficiently.
- Character Arithmetic: How to convert lowercase characters into array indexes using
s[i] - 'a'. - 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:
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#.
Join the conversation! Your thoughts help the community grow.