5. Count number of substrings
The problem can be found at the following link: Question Link
My Approach
To count the number of substrings in a given string s
with exactly k
distinct characters, we can break it down into two steps:
First, find the number of substrings with at most
k
distinct characters.Then, subtract the number of substrings with at most
k-1
distinct characters from the above count.
I used a sliding window approach (similar to moving two pointers) to achieve this. Here's how it works:
I kept a count called
cnt
to keep track of the valid substrings and used an array calledfreq
(with 26 slots, one for each alphabet letter) to keep track of the frequency of characters in the current window.I had two pointers,
i
andj
, initially set to the start of the string. Additionally, I used a variable calleddiff
to count the number of distinct characters in the current window.I went through the string from left to right using the pointer
j
.For each character
s[j]
, I increased its frequency in thefreq
array and checked if it was not in the current window before (meaningfreq[s[j] - 'a'] == 1
). If so, I incremented thediff
by 1, indicating the addition of a new distinct character.After adding the character
s[j]
, I checked ifdiff
was greater thank
.If it was, I moved the
i
pointer from the left end of the window, decreased the frequency of the character ats[i]
, and checked if its frequency became zero (i.e.,freq[s[i] - 'a'] == 0
). If so, I decreaseddiff
by 1, showing that a distinct character was removed from the window.
At each step, I added
j - i + 1
tocnt
, representing the count of valid substrings ending ats[j]
.
Finally, I returned the value of
cnt
, which represents the count of valid substrings with at mostk
distinct characters.
Time and Auxiliary Space Complexity
Time Complexity: The algorithm iterates through the string once with two pointers, so the time complexity is
O(N)
, whereN
is the length of the input strings
.Auxiliary Space Complexity: The algorithm uses an array
freq
of size26
to store the frequency of characters, which is a constant space requirement. Therefore, the auxiliary space complexity isO(1)
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated