Input: s ="baeyh", k =2Output: 2Explanation: There are 2 beautiful substrings in the given string.- Substring "b _aeyh_ ", vowels =2(["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "_baey_ h", vowels = 2 (["a",e"]), consonants =2(["b","y"]).You can see that string "baey"is beautiful as vowels == consonants and vowels * consonants % k ==0.It can be shown that there are only 2 beautiful substrings in the given string.
Input: s ="abba", k =1Output: 3Explanation: There are 3 beautiful substrings in the given string.- Substring "_ab_ ba", vowels =1(["a"]), consonants =1(["b"]).- Substring "ab _ba_ ", vowels =1(["a"]), consonants =1(["b"]).- Substring "_abba_ ", vowels =2(["a","a"]), consonants =2(["b","b"]).It can be shown that there are only 3 beautiful substrings in the given string.
We want to count substrings where the number of vowels equals the number of consonants, and their product is divisible by k. For large k, we can optimize by only considering substring lengths that are multiples of k and use prefix sums to efficiently count valid substrings.
classSolution {
public:int beautifulSubstrings(string s, int k) {
unordered_set<char> vowels = {'a','e','i','o','u'};
int n = s.size(), ans =0;
vector<int> vcnt(n+1), ccnt(n+1);
for (int i =0; i < n; ++i) {
vcnt[i+1] = vcnt[i] + vowels.count(s[i]);
ccnt[i+1] = ccnt[i] + (!vowels.count(s[i]));
}
for (int len =2; len <= n; len +=2) {
if ((len/2)*(len/2) % k !=0) continue;
for (int i =0; i + len <= n; ++i) {
int v = vcnt[i+len] - vcnt[i];
int c = ccnt[i+len] - ccnt[i];
if (v == c && v * c % k ==0) ans++;
}
}
return ans;
}
};
classSolution {
publicintbeautifulSubstrings(String s, int k) {
Set<Character> vowels = Set.of('a','e','i','o','u');
int n = s.length(), ans = 0;
int[] vcnt =newint[n+1], ccnt =newint[n+1];
for (int i = 0; i < n; ++i) {
vcnt[i+1]= vcnt[i]+ (vowels.contains(s.charAt(i)) ? 1 : 0);
ccnt[i+1]= ccnt[i]+ (vowels.contains(s.charAt(i)) ? 0 : 1);
}
for (int len = 2; len <= n; len += 2) {
if ((len/2)*(len/2) % k != 0) continue;
for (int i = 0; i + len <= n; ++i) {
int v = vcnt[i+len]- vcnt[i];
int c = ccnt[i+len]- ccnt[i];
if (v == c && v * c % k == 0) ans++;
}
}
return ans;
}
}
classSolution {
funbeautifulSubstrings(s: String, k: Int): Int {
val vowels = setOf('a','e','i','o','u')
val n = s.length
val vcnt = IntArray(n+1)
val ccnt = IntArray(n+1)
for (i in0 until n) {
vcnt[i+1] = vcnt[i] + if (s[i] in vowels) 1else0 ccnt[i+1] = ccnt[i] + if (s[i] in vowels) 0else1 }
var ans = 0for (len in2..n step 2) {
if ((len/2)*(len/2) % k !=0) continuefor (i in0..n-len) {
val v = vcnt[i+len] - vcnt[i]
val c = ccnt[i+len] - ccnt[i]
if (v == c && v * c % k ==0) ans++ }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defbeautifulSubstrings(self, s: str, k: int) -> int:
vowels = set('aeiou')
n = len(s)
vcnt = [0] * (n+1)
ccnt = [0] * (n+1)
for i in range(n):
vcnt[i+1] = vcnt[i] + (s[i] in vowels)
ccnt[i+1] = ccnt[i] + (s[i] notin vowels)
ans =0for l in range(2, n+1, 2):
if (l//2)*(l//2) % k !=0:
continuefor i in range(n-l+1):
v = vcnt[i+l] - vcnt[i]
c = ccnt[i+l] - ccnt[i]
if v == c and v * c % k ==0:
ans +=1return ans
impl Solution {
pubfnbeautiful_substrings(s: String, k: i32) -> i32 {
let vowels = [b'a', b'e', b'i', b'o', b'u'];
let n = s.len();
let s = s.as_bytes();
letmut vcnt =vec![0; n+1];
letmut ccnt =vec![0; n+1];
for i in0..n {
vcnt[i+1] = vcnt[i] +if vowels.contains(&s[i]) { 1 } else { 0 };
ccnt[i+1] = ccnt[i] +if vowels.contains(&s[i]) { 0 } else { 1 };
}
letmut ans =0;
for len in (2..=n).step_by(2) {
if (len/2)*(len/2) % k !=0 { continue; }
for i in0..=n-len {
let v = vcnt[i+len] - vcnt[i];
let c = ccnt[i+len] - ccnt[i];
if v == c && v * c % k ==0 {
ans +=1;
}
}
}
ans
}
}