[LeetCode] Number of Equal Count Substrings

2067. Number of Equal Count Substrings

You are given a 0-indexed string s consisting of only lowercase English letters, and an integer count. A substring of s is said to be an equal count substring if, for each unique letter in the substring, it appears exactly count times in the substring.

Return the number of equal count substrings in s.

A substring is a contiguous non-empty sequence of characters within a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int equalCountSubstrings(string s, int count) {
int res = 0, n = s.length();
for(int unique = 1; unique <= 26; unique++) {
int len = unique * count, now = 0;
unordered_map<char, int> freq;
for(int i = 0; i < n; i++) {
if(++freq[s[i]] == count) now++;
if(i >= len and --freq[s[i-len]] == count - 1) now--;
res += now == unique;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/09/PS/LeetCode/number-of-equal-count-substrings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.