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 ofequal count substrings in _s.
A substring is a contiguous non-empty sequence of characters within a string.
Input: s ="aaabcbbcc", count =3Output: 3Explanation:
The substring that starts at index 0 and ends at index 2is"aaa".The letter 'a'in the substring appears exactly 3 times.The substring that starts at index 3 and ends at index 8is"bcbbcc".The letters 'b' and 'c'in the substring appear exactly 3 times.The substring that starts at index 0 and ends at index 8is"aaabcbbcc".The letters 'a','b', and 'c'in the substring appear exactly 3 times.
Example 2:
1
2
3
4
5
Input: s ="abcd", count =2Output: 0Explanation:
The number of times each letter appears in s is less than count.Therefore, no substrings in s are equal count substrings, so return0.
Example 3:
1
2
3
4
5
Input: s ="a", count =5Output: 0Explanation:
The number of times each letter appears in s is less than count.Therefore, no substrings in s are equal count substrings, so return0
#include<vector>#include<string>usingnamespace std;
classSolution {
public:int equalCountSubstrings(string s, int count) {
int n = s.size(), ans =0;
for (int unique =1; unique <=26; ++unique) {
int len = unique * count;
if (len > n) break;
vector<int> freq(26, 0);
int diff =0;
for (int i =0; i < len; ++i) {
int idx = s[i]-'a';
if (freq[idx] ==0) diff++;
freq[idx]++;
}
if (diff == unique && all_of(freq.begin(), freq.end(), [&](int f){ return f ==0|| f == count; })) ans++;
for (int i = len; i < n; ++i) {
int out = s[i-len]-'a', in = s[i]-'a';
freq[out]--;
if (freq[out] ==0) diff--;
if (freq[in] ==0) diff++;
freq[in]++;
if (diff == unique && all_of(freq.begin(), freq.end(), [&](int f){ return f ==0|| f == count; })) ans++;
}
}
return ans;
}
};
classSolution {
publicintequalCountSubstrings(String s, int count) {
int n = s.length(), ans = 0;
for (int unique = 1; unique <= 26; ++unique) {
int len = unique * count;
if (len > n) break;
int[] freq =newint[26];
int diff = 0;
for (int i = 0; i < len; ++i) {
int idx = s.charAt(i)-'a';
if (freq[idx]== 0) diff++;
freq[idx]++;
}
if (diff == unique && check(freq, count)) ans++;
for (int i = len; i < n; ++i) {
int out = s.charAt(i-len)-'a', in = s.charAt(i)-'a';
freq[out]--;
if (freq[out]== 0) diff--;
if (freq[in]== 0) diff++;
freq[in]++;
if (diff == unique && check(freq, count)) ans++;
}
}
return ans;
}
booleancheck(int[] freq, int count) {
for (int f : freq) if (f != 0 && f != count) returnfalse;
returntrue;
}
}
classSolution {
funequalCountSubstrings(s: String, count: Int): Int {
val n = s.length
var ans = 0for (unique in1..26) {
val len = unique * count
if (len > n) breakval freq = IntArray(26)
var diff = 0for (i in0 until len) {
val idx = s[i]-'a'if (freq[idx] ==0) diff++ freq[idx]++ }
if (diff == unique && freq.all { it==0||it== count }) ans++for (i in len until n) {
val out = s[i-len]-'a'; val inx = s[i]-'a' freq[out]--if (freq[out] ==0) diff--if (freq[inx] ==0) diff++ freq[inx]++if (diff == unique && freq.all { it==0||it== count }) ans++ }
}
return ans
}
}
classSolution:
defequalCountSubstrings(self, s: str, count: int) -> int:
n, ans = len(s), 0for unique in range(1, 27):
length = unique * count
if length > n: break freq = [0]*26 diff =0for i in range(length):
idx = ord(s[i])-ord('a')
if freq[idx] ==0: diff +=1 freq[idx] +=1if diff == unique and all(f ==0or f == count for f in freq): ans +=1for i in range(length, n):
out, inx = ord(s[i-length])-ord('a'), ord(s[i])-ord('a')
freq[out] -=1if freq[out] ==0: diff -=1if freq[inx] ==0: diff +=1 freq[inx] +=1if diff == unique and all(f ==0or f == count for f in freq): ans +=1return ans
impl Solution {
pubfnequal_count_substrings(s: String, count: i32) -> i32 {
let n = s.len();
let s = s.as_bytes();
letmut ans =0;
for unique in1..=26 {
let len = unique * count asusize;
if len > n { break; }
letmut freq =vec![0; 26];
letmut diff =0;
for i in0..len {
let idx = (s[i]-b'a') asusize;
if freq[idx] ==0 { diff +=1; }
freq[idx] +=1;
}
if diff == unique && freq.iter().all(|&f| f ==0|| f == count) { ans +=1; }
for i in len..n {
let out = (s[i-len]-b'a') asusize;
let inx = (s[i]-b'a') asusize;
freq[out] -=1;
if freq[out] ==0 { diff -=1; }
if freq[inx] ==0 { diff +=1; }
freq[inx] +=1;
if diff == unique && freq.iter().all(|&f| f ==0|| f == count) { ans +=1; }
}
}
ans
}
}